tags:

views:

98

answers:

4

I have situation where in the regular expression is something like this: ^b?A+b?$

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

Now I want to modify this regular expression in such way that it may match b either at the start or at the end of the string, but not both.

How do I do this?

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

why doesn't that work?

Dan Beam
I thinks he wants bbAAA _or_ AAAbb.
Bobby
Actually b can repeat 0 or 1 times, and not more than 1 times
Ngm
@Ngm: the word "repeat" is misleading; see how I edited your question for a better way to express your goal (I hope). @Dan: in addition to the "0 or 1" issue, your regex matches 'b' at both the beginning and the end of the string, which the OP doesn't want.
Alan Moore
bad instructions if you had to edit his post, IMO
Dan Beam
+2  A: 

Try this:

^(bA+|A+b?)$

This allows a b at the start and then at least one A, or some As at the start and optionally a b at the end. This covers all the possibilities and is slightly faster than the accepted answer in the case that it doesn't match as only one of the two options needs to be tested (assuming A cannot begin with b).

Just to be different from the other answers here, if the expression A is quite complex but b is simple, then you might want to do it using a negative lookahead to avoid repeating the entire expression for A in your regular expression:

^(b(?!.*b$))?A+b?$

The second might be more readable if your A is complex, but if performance is an issue I'd recommend the first method.

Mark Byers
Now `^` applies only to first part of the `|` and `$` applies only to the second part.
Amarghosh
@Amarghosh: Fixed, thanks....
Mark Byers
+9  A: 

Theres a nice "or" operator in regexps you can use.

^(b?A+|A+b?)$
Dietrich Epp
This is what I wanted, thanks guys for all your answers
Ngm
+1, this works too, and you got it right on the first attempt. :)
Mark Byers
+3  A: 

Although the other answers have covered the regex portion, for future reference, I find it can be easier to visualise these problems as the state machine the regex represents;

^b?A+b?$

= b can repeat at the start of the expression 0 or 1 times, and A can repeat more than once. Again b can repeat at the end of the expression 0 or 1 times.

= 1 b, infinite a, 0 b
OR 0 b, infinite a, 0 b
OR 0 b, infinite a, 1 b

Automata1


"Now I want to modify this regular expression in such way that it will either allow repetition of "b" at the start or at the end, but not both."

Automata2

This is "^(bA+|A+b?)$". You can see the top branch is "bA+", then the bottom branch "A+b?".

RJFalconer