Regular expression: How to choose something that matches the beginning or end, but is not both?

I have this situation: the regular expression is like this:
^ b? A b? $

So b can match at the beginning of the string 0 or 1, and A must match one or more times. Again b can match 0 or 1 times at the end of the string.

Now I want to modify this regular expression in such a way that it can match b at the beginning or end of the string, but not b at the same time.

What should I do?

You can use a beautiful “or” operator in regexp.

^(b?A+|A+b?)$

I have this situation: the regular expression is like this Of:
^ b? A b? $

So b can match at the beginning of the string 0 or 1, and A must match one or more times. Again b can match 0 or 1 times at the end of the string.

Now I want to modify this regular expression in such a way that it can match b at the beginning or end of the string, but not b at the same time.

What should I do?

You can use a beautiful “or” operator in regexp.

 ^(b?A+|A+b?)$

Leave a Comment

Your email address will not be published.