J. Doe
J. Doe

Reputation: 306

Regular Expression: Having All strings except bba and abb

I was working on Regular Expression, There was a question about making a Regular Expression having string containing at-least one of among bba or abb but not both at the same time.

I made below expression for that.

a*(baa*)b+b(a*ab)a

But, A question arise in my mind to make a regular expression having All strings excep bba and abb

I am confuse in it now.

Kindly help.

Note: Its about Automata, Not JavaScript Or PHP Regex ........

Upvotes: 1

Views: 1704

Answers (3)

Stephan T.
Stephan T.

Reputation: 6074

If it is an automata, wouldnt the answer just be

b(aa*b + a)* + (aa*b + a)* + b*         
// First one:  words like baaaaabaababab, baaa, babaaaaaa 
// Second one: words like aaaaabaababab, aaaaaa, aab
// Third one:  words like bbbbb, b, , bbb

because you can't type bb unless it is the only word in the regex

a^+ means at least one a and can be replaced by aa*

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163362

You could use an alternation with a positive and negative lookahead to assert that one of the variations is not there and that the other variation is and vice versa.

^(?:(?=.*abb)(?!.*bba)|(?!.*abb)(?=.*bba)).*$

Regex demo

If neither of them could be there, you could use:

^(?!.*(?:abb|bba)).*$

Upvotes: 1

Nambi_0915
Nambi_0915

Reputation: 1091

Try regex ^(?!(.*abb.*bba|.*bba.*abb)).*$

This will match the string contains either abb or bba not both

Regex

Upvotes: 0

Related Questions