tags:

views:

71

answers:

2

Looking for a regex string that would match all alpha numeric character plus a few special ones that include + and -. Our current regex looks like this:

^[a-zA-Z0-9_,\.\-' ]+$

However, we need to also include the + and - as well so long as they are not surrounded by whitespace on either side. For example an A+ BC would match, but not A + BC.

Tried using word boundaries and haven't been successful. Any ideas?

+1  A: 

How about:

(([^ ]|^)[-+] ?| ?[-+]([^ ]|$))

That should match any - or + which has some non-space character on either one of the sides (so A+ BC, - or A -BC).

If you want to include alphanumerics in that, you could do it like this:

((([^ ]|^)[-+] ?| ?[-+]([^ ]|$))|[a-zA-Z...]+)

And then repeat it:

^((([^ ]|^)[-+] ?| ?[-+]([^ ]|$))|[a-zA-Z...]+)+$

This will match, for example, A+ B -C and A+B, but not A - B.

David Wolever
I'll give it a shot. Thanks!
FlyingFish
The use of `[^ ]` opens the door to other characters. For example, your regex accepts `:+123`, or `a|+3`.
Kobi
D'oh! Alright, well, that should be the `[a-zA-Z0-9...]` sequence then.
David Wolever
I substituted my character class for yours, so it's still good.
FlyingFish
+1  A: 

As said in the comments, you want to accept blocks of + and -, but only if there's a character on either side. This seems to work well:

^([\w\s.,']|\b[+-]+|[+-]+\b)+$

Explanation:

This regex can only take + or - if it can find a matching character before or after them.

One or more of:
[\w\s.,'] - Alphanumeric, white space, dot, comma or apostrophe. OR
\b[+-]+ - Alphanumeric, dot, comma or apostrophe, followed by a block of +- OR
[+-]+\b - A block of +- followed by an alphanumeric, dot, comma or apostrophe.

Test cases:

Accepts: a+ bv. a+2 A+ B -C A+B +1 -2 2+++-1 +1 ++3 2++++ 1++ 2

Rejects: A + BC a|+3 >+a A - B 2+++-1 +1 ++ ++++ +

Other:

Word boundaries may don't play well here because a + is a word boundary. It did work on RegExr, but your flavor may be different. This will also work: ^([\w\s.,']|[\w.,'][+-]+|[+-]+[\w.,'])+$

Kobi
This worked as well but I think this is easier to understand and maintain without introducing errors.
FlyingFish