Search code examples
regexautomata

Regex | Containing "bbb"


I'm trying to create a Regex with chars 'a' and 'b'. The only rule is that the regex must contain the word 'bbb' somewhere.

These are possible: aabbbaaaaaababa, abbba, bbb, aabbbaa, abbabbba, ... These are not possible: abba, a, abb, bba, abbaaaabbaaaabba, ...

I have no idea how can I can express that.

Any ideas? Thanks in Advance!


Solution

  • Based on the tag "automata", I am guessing you are after the formal regular expression for this formal language. In that case, a regular expression is (a+b)bbb(a+b). The anatomy of this regular expression is the following:

    • (a+b) gives either "a" or "b"
    • (a+b)* gives any string of "a"s and "b"s whatever
    • bbb gives the string bbb only
    • the whole regular expression describes any string that begins with anything, then has bbb, then ends with anything

    To prove this regular expression is correct, note that:

    • This regular expression only generates strings that contain the substring bbb. This is due to the middle part.
    • This regular expression generates all strings that contain the substring bbb. Suppose there were some string containing the substring bbb that this regular expression didn't generate. The string either starts with bbb or it doesn't. If it does, then the string is generated by our regular expression by repeating the first (a+b) zero times and the second (a+b) n - 3 times, where n is the length of the string. Otherwise, if it doesn't start with bbb, consider the suffix of length n - 1 as a recursive case. Continue thusly until the subcase does begin with bbb (it eventually must). Because this suffix is describable by our regular expression, the original case must be too since we can just repeat the first (a+b) an additional number of times equal to the depth of recursion.