-2

This is a simple question of the Theory of Computation. I don't know nor want the python coded interpretation of this but rather the theoretical answer of the expression. I have tried my best to figure it out and came up with the below code:

 (ab+ba+bb)*. aa.(ab+ba+bb)*.aa.(ab+ba+bb)* + b*.aa.b*.aa.b*

Is it right? Am I forgetting any other case?

  • 1
    Your sample looks weird. Should this be two regex patterns left and right of the `+` rightmost? Also the use of [unescaped](https://stackoverflow.com/questions/5105143/list-of-all-characters-that-should-be-escaped-before-put-in-to-regex) `.` is confusing. – bobble bubble Sep 08 '18 at 09:06

1 Answers1

3

Your regex is too complicated and not very flexible (it only works with strings of a and b). A better solution uses negative look-ahead assertions:

^(?:(?!aa).)*aa(?:(?!aa).)*aa(?:(?!aa).)*$

This looks for any length of substring at the start of the string that does not contain aa, then the first aa, and so on.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • A possible simplification: `^(?:(?:(?!aa).)*aa){2}(?:(?!aa).)*$` http://regexr.com/3v54r – morja Sep 08 '18 at 13:52
  • I don't want the python(coding) version of this. I'm simply a student who wants to know the regular expression that accepts the problem in hand. Also, my set of symbols is {a, b}. – Meghna Tanwal Oct 02 '18 at 09:31
  • @MeghnaTanwal My answer isn’t limited to Python. It should solve your problem. If it helped you, please remember to accept it. – elixenide Oct 02 '18 at 12:40