Fabian
Fabian

Reputation: 434

Python Regex Alternative

I am trying to match the following language:

L = {a^nb^m | (n+m) is even }

That is, n a's followed by m b's so that (n+m) is even. I build the following regex

((aa)*(bb)*)|(a(aa)*b(bb)*)

but it only ever matches the first part of the alternative, not the second (tested with https://regex101.com/r/c5HucF/1/). It won't match aaabbb. When I remove the first alternative, leaving me only with (a(aa)*b(bb)*), aaabbb is matched just fine. What am I missing?

Upvotes: 1

Views: 97

Answers (1)

JvdV
JvdV

Reputation: 75840

Looks like the main criteria here is that n+m is an even number, therefor you could try:

^(?=(?:..)+$)a+b+$

See the online demo

  • ^ - Start string anchor.
  • (?= - Positive lookahead:
    • (?: - Non capture group:
      • .. - Any two characters other than newline.
      • )+$ - Close non-capture group and match 1+ time up to end string.
    • ) - Close positive lookahead.
  • a+b+ - 1+ times an "a" and 1+ times a "b".
  • $ - End string anchor.

I guess you could change the + in a+b+ into an * if you would wish to match "bb" to where "a^0" would be considered valid input.

Upvotes: 2

Related Questions