5

I convert a excel file to a CSV , and that to a String.

The problem is that a regular expression is not working correctly

I want to detect this kind of text:

MYW Pkg, MYW Pkg + Quick Service Dining, MYW Pkg + Dining, MYW Pkg + Deluxe Dining,

Room + Tickets + Quick Service Dining

I have an array of String. So I need to know a pattern for that, I try this but it doesn't detect it:

Pattern.compile("([A-Z]{3})+(\\s)+([A-Za-z]{3})+(\\s)+(\\+)");

I try to match "MYW Pkg +" for example, Do you know why it is not working?

More code:

chain is the array with values like "MYW Pkg,"

Pattern patPackageDescription = Pattern.compile("([A-Z]{3})+(\\s)+([A-Za-z])+(\\s)+(\\+)");
        for (int i = 0; i < chain.length; i++) {
            Matcher matPackageDescription = patPackageDescription
                    .matcher(chain[i]);

            if (matPackageDescription.matches()) {
                String space = String.format("%1$-" + 50 + "s",
                        chain[i].toString());
                a.append(space + "|\n");
            }
        }

Regards.

7
  • 1
    What parts of the above string are you trying to match. Two things I will point out are 1) (?) is weird / most likely not necessary and 2) you're looking for [A-Z]+ right before \\+ (even though there's a space between Pkg and +). Commented Jul 19, 2016 at 19:49
  • @Sam I try to match "MYW Pkg +" at least, and anubhava (?) is not for case sensitive? Commented Jul 19, 2016 at 19:56
  • 2
    Inline case insensitive modifier will look like (?i:[A-Z]), but you can just use [A-Za-z] Commented Jul 19, 2016 at 19:57
  • Still not reaching any result :( I added the regExp to the question Commented Jul 19, 2016 at 20:01
  • show us a little bit more of your code Commented Jul 19, 2016 at 20:01

2 Answers 2

11

matches() method tries to match the whole string against the pattern, to match a part of the string you need to use find() method.

String str = "MYW Pkg, MYW Pkg + Quick Service Dining, MYW Pkg + Dining, MYW Pkg + Deluxe Dining,";
Pattern patPackageDescription = Pattern.compile("([A-Za-z]{3}\\s)+\\+");
Matcher matPackageDescription = patPackageDescription.matcher(str);

while (matPackageDescription.find()) {
    System.out.println(matPackageDescription.group());
}

Outputs:

MYW Pkg +
MYW Pkg +
MYW Pkg +

Look here for an explanation.

Sign up to request clarification or add additional context in comments.

6 Comments

Is correct what you say I add (.*) for a correct match .. so ([A-Za-z]{n}\\s)+(\\+)+([A-Za-z]{n}\\s)+(\\+)+(.*) That should serve for these cases: MYW Pkg + Quick Service Dining , Room + Tickets + Quick Service Dining..... Right?
How can I place n for any number?
@arnoldssss, tell me what exactly do you need to do =) So I can help you than.
I need to detect in my array of String things like: Room + Tickets + Quick Service Dining, MYW Pkg + Quick Service Dining. So I need to know ... why this regex is not working "([A-Za-z]{1,}\\s)+(\\+)+([A-Za-z]{1,}\\s)+(\\+)+(.*)"
oh I see my issue now.. htere are differences in both Strings... I´ll give your answer as good.. thanks
|
2

Your problem is that you are using Matcher.matches() which requires a full match, if you can either use find() for partial matches or add .* to match anything after your search string.

([A-Z]{3})+(\s)+([A-Za-z]{3})+(\s)+(\+).*

Regular expression visualization

Debuggex Demo

2 Comments

cool I add it .. so ([A-Za-z]{n}\\s)+(\\+)+([A-Za-z]{n}\\s)+(\\+)+(.*) That should serve for these cases: MYW Pkg + Quick Service Dining And Room + Tickets + Quick Service Dining Right?
if your string will start from something like number, it will fail. you need to add .*? to the beginning of your regex too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.