Reputation: 33
I'm trying to create a regex that matches a repeating pattern or two or more characters (excluding a single repeating character) three or more times.
(.)\1{3,}
will match a single repeated character e.g. matches aaaa
but not ababab
or abcde
(.+)\1{3,}
will match a repeated patern e.g. aaa
, ababab
, ab ab ab
but not abcde
I want to match only ababab
, ab ab ab
etc
but not aaaa
or abcde
How do I solve this problem?
Upvotes: 3
Views: 1365
Reputation: 18357
As you want to discard matching if all the characters are same, you can use negative lookahead for rejecting that. In otherwise case, you can change +
to {2,}
in the quantifier where you are capturing the first part and then use {2,}
so your matched first part repeats three or more times. You can use this regex,
^(?!(.)\1+$)(.{2,})\2{2,}$
Explanation:
^
- Start of string(?!(.)\1+$)
- This negative look ahead ensures if the string consists of only one character till end of string, then it rejects the match.(.{2,})
- Match two or more any character and capture it in group2\2{2,}
- Repeat what was captured in group2 two or more times$
- End of stringAlso, from your post, as you said you don't want to match aaaa
since it has aa
pattern repeated only two times and not three or more times, so if I am correct and you consider aaaaaa
as a successful match, because now it has aa
repeating three times, in that case you can get rid of negative lookahead from my above regex and use this regex which will not match aaaa
but will match aaaaaa
like I explained above.
^(.{2,})\1{2,}$
Let me know if this is indeed what you wanted. In case of any queries, feel free to drop your comment.
Upvotes: 2
Reputation: 274520
You can use this regex:
^((.)\2*?(?!\2).+?)\1{2,}$
The first part ((.)\2*?(?!\2).+?)
matches a string that has at least 2 different characters in it, then it repeats it at least 2 more times \1{2,}
.
Breaking the first part down, we first match the first character, put it in group 2. Then we allow this character to repeat a few times, or not (\2*
). Then we assert that there must be something else other than this character (?!\2)
. And then we lazily match the rest .+?
.
Upvotes: 2