Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplifying a Regular Expression

Tags:

java

regex

I have a really simple question (punny right).

What is the simplest form of this regular expression?

(((0|1)*(00)(0|1)*)((0|1)*(11)(0|1)*))|(((0|1)*(11)(0|1)*)((0|1)*(00)(0|1)*))

I'm creating a regular expression that accepts a language for all binary strings that contain the substrings 00 and 11 (in any order).

The expression I have now works, but I'm sure it can be simplified.

like image 437
kmaz13 Avatar asked Feb 26 '26 20:02

kmaz13


1 Answers

This is almost the same regex. I only converted the (0|1) into [01], added a [01]* to the left and right shared for both cases (11 first or 00 first) and removed some parenthesis that were not necessary:

[01]*(00[01]*11|11[01]*00)[01]*

Steps to reproduce

  1. Stating with

    (((0|1)*(00)(0|1)*)((0|1)*(11)(0|1)*))|(((0|1)*(11)(0|1)*)((0|1)*(00)(0|1)*))
    __^^^^^_____^^^^^___^^^^^_____^^^^^______^^^^^_____^^^^^___^^^^^_____^^^^^___

  2. Replace all (0|1) by [01]

    (([01]*(00)[01]*)([01]*(11)[01]*))|(([01]*(11)[01]*)([01]*(00)[01]*)) _______^^^^____________^^^^_______________^^^^____________^^^^_______

  3. Remove parenthesis around (00) and (11) as you do not want to capture this group and you do not have a *, +, ? behind the parenthesis. Therefore it is not required because of ambiguity.

    (([01]*00[01]*)([01]*11[01]*))|(([01]*11[01]*)([01]*00[01]*))
    _^____________^^____________^___^____________^^____________^_

  4. Remove yet more parenthesis that does not solve any ambiquity:

    ([01]*00[01]*[01]*11[01]*)|([01]*11[01]*[01]*00[01]*)
    ________^^^^^^^^^^_________________^^^^^^^^^^________

  5. Collapse [01]*[01]* into [01]* which means exactly the same.

    ([01]*00[01]*11[01]*)|([01]*11[01]*00[01]*)
    _^^^^^_________^^^^^___^^^^^_________^^^^^_

  6. Extract common prefix and suffix [01]*

    [01]*(00[01]*11|11[01]*00)[01]*

like image 79
bikeshedder Avatar answered Feb 28 '26 08:02

bikeshedder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!