SachiraChin
SachiraChin

Reputation: 570

Repeat pattern in RegEx

I've got an string parts which match to following pattern.

abcd|(|a|ab|abc)e(fghi|(|f|fg|fgh)jklmn)

But problem I have got is, my whole string is repeated combination of above like patterns. And my whole string must contain more than 14 sets of above pattern.
Can anyone one help me to improve my above RegEx to wanted format.

Thanks

Update
Input examples:
Matched string parts : abcd, abefgjkln, efjkln, ejkln
But whole string is : abcdabefgjklnefjklnejkln (Combination of above 4 parts)

There must be more than 15 parts in whole string. Above one have only 4 parts. So, it's wrong.

Upvotes: 0

Views: 4903

Answers (3)

poplitea
poplitea

Reputation: 3727

What about this:

(?:a(?:b(?:c(?:d)?)?)?ef(?:g(?:h(?:i)?)?)?jklmn){15,}

Explanation: you create a non-capturing group (with (?: ... )), and say that this should be repeated >=15 times, hence the curly braces in the end.

Upvotes: 1

FailedDev
FailedDev

Reputation: 26930

This will try to match your "parts" at least 15 times in a string.

    boolean foundMatch = false;
    try {
        foundMatch = subjectString.matches("(?:(?:ab(?:cd|efgjkln))|(?:(?:ef?jkln))){15,}");
    } catch (PatternSyntaxException ex) {
        // Syntax error in the regular expression
    }

If there are at least 15 repetitions of any of the above parts foundMatch will be true, else it will remain false.

Breakdown :

"(?:" +                       // Match the regular expression below
   "|" +                         // Match either the regular expression below (attempting the next alternative only if this one fails)
      "(?:" +                       // Match the regular expression below
         "ab" +                        // Match the characters “ab” literally
         "(?:" +                       // Match the regular expression below
                                          // Match either the regular expression below (attempting the next alternative only if this one fails)
               "cd" +                        // Match the characters “cd” literally
            "|" +                         // Or match regular expression number 2 below (the entire group fails if this one fails to match)
               "efgjkln" +                   // Match the characters “efgjkln” literally
         ")" +
      ")" +
   "|" +                         // Or match regular expression number 2 below (the entire group fails if this one fails to match)
      "(?:" +                       // Match the regular expression below
         "(?:" +                       // Match the regular expression below
            "e" +                         // Match the character “e” literally
            "f" +                         // Match the character “f” literally
               "?" +                         // Between zero and one times, as many times as possible, giving back as needed (greedy)
            "jkln" +                      // Match the characters “jkln” literally
         ")" +
      ")" +
"){15,}"                      // Between 15 and unlimited times, as many times as possible, giving back as needed (greedy)

Upvotes: 5

AlexR
AlexR

Reputation: 115328

First, it seems that your pattern can be simplified. Really pattern a is a subset of ab that is a subset of abc, so if pattern abc matches it means that a matches too. Think about this and change your pattern appropriately. Right now it probably not what you really want.

Second, to repeat something is puttern use {N}, i.e. abc{5} means "abc repeated five times". You can also use {3,}, {,5}, {3,5} that mean repeat>=3, repeat<=5, 3<=repeat<=5.

Upvotes: -1

Related Questions