Reputation: 1434
I need to match over the alphabet {a,b} (meaning that we can discard any other letter since only a
and b
will exist):
a
s.bbb
.Why is this RegEx:
((b{0,2}aaa*)+)|((aaa*b{0,2})+)
Not capturing aab
?
Upvotes: 0
Views: 64
Reputation: 7351
Your requirements:
All strings containing 2 or more a's. All strings that do not contain the substring bbb.
seem to argue for a simpler, lookahead based approach, instead of the trickier consuming pattern (depends on your exact workflow):
(?=a.*a)(?!.*bbb).*
edit: to exclude all letters except a and b:
^(?=.*a.*a)(?!.*bbb)[ab]+$
Upvotes: 0
Reputation: 174696
Because aa
got captured by your first pattern. To get the desired output, you need to change the pattern order.
((aaa*b{0,2})+)|((b{0,2}aaa*)+)
Note that regex engine always try to match the input against the pattern which resides on the left side then it goes further to the right side. So it would be like,
1st|2nd|3rd
Update:
^(?!.*?bbb).*a.*a.*
Upvotes: 1