Home > OS >  Need regular expression for All strings over the alphabet {a, b} that do not contain the substring a
Need regular expression for All strings over the alphabet {a, b} that do not contain the substring a

Time:02-24

Can I get a general tip about how to do these questions ? I've been looking at a lot of examples, is there a method or mathematical formula I could follow to help me arrive at the answer ? Seems like there is no way to do this instead of just knowing what to do

my solution has been (bba|bab)*bb*

Here is the answer I've arrived at now:

( b | ab )*b

CodePudding user response:

You can match the regular expression

^(?!.*(?:aab|bb$))[ab]*$

Demo

The expression can be broken down as follows.

^        # match beginning of string
(?!      # begin negative lookahead
  .*     # match zero or more characters
  (?:    # begin non-capture group
    aab  # match literal
    |    # or
    bb$  # match literal followed by end of string
  )      # end non-capture group
)        # begin negative lookahead
[ab]*    # match zero or more characters, each 'a' or 'b'
$        # match end of string

.* matches the entire line if and only if the negative lookahead succeeds (that is, if and only if the string does not contain 'aab' and does not contain 'bb' at the end of the string).



My understanding is that one requirement is that the string cannot end 'bb'. If the string must end 'bb', use the following regular expression:

^(?!.*aab)[ab]*bb$

CodePudding user response:

Try

^(?=[ab] )(?:(?!aab)[ab])*bb$
  • Related