ocram
ocram

Reputation: 1434

Regex Not Capturing input

I need to match over the alphabet {a,b} (meaning that we can discard any other letter since only a and b will exist):

  1. All strings containing 2 or more as.
  2. All strings that do not contain the substring bbb.

Why is this RegEx:

((b{0,2}aaa*)+)|((aaa*b{0,2})+)

Not capturing aab?

Upvotes: 0

Views: 64

Answers (3)

Scott Weaver
Scott Weaver

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).*

regex demo

edit: to exclude all letters except a and b:

^(?=.*a.*a)(?!.*bbb)[ab]+$

regex demo

Upvotes: 0

ocram
ocram

Reputation: 1434

This seems to work too:

(ab{0,2}a)(ab{0,2})*

Upvotes: 0

Avinash Raj
Avinash Raj

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.*

DEMO

Upvotes: 1

Related Questions