4

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.

arnoldssss
  • 468
  • 3
  • 9
  • 22

2 Answers2

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.

Community
  • 1
  • 1
spirit
  • 3,265
  • 2
  • 14
  • 29
  • 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? – arnoldssss Jul 19 '16 at 20:29
  • How can I place n for any number? – arnoldssss Jul 19 '16 at 20:30
  • @arnoldssss, tell me what exactly do you need to do =) So I can help you than. – spirit Jul 19 '16 at 20:36
  • 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)+(\\+)+(.*)" – arnoldssss Jul 19 '16 at 20:40
  • oh I see my issue now.. htere are differences in both Strings... I´ll give your answer as good.. thanks – arnoldssss Jul 19 '16 at 20:43
  • you are welcome. But I think, you use overcomplicated regex's =). For example, you don't need parenthesises that much. – spirit Jul 19 '16 at 20:50
1

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

Leonard Brünings
  • 12,408
  • 1
  • 46
  • 66
  • 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? – arnoldssss Jul 19 '16 at 20:27
  • if your string will start from something like number, it will fail. you need to add `.*?` to the beginning of your regex too – spirit Jul 19 '16 at 20:30