tags:

views:

59

answers:

3

Hi,

I need a regular expression to list accepted Version Numbers. ie. Say I wanted to accept "V1.00" and "V1.02". I've tried this "(V1.00)|(V1.01)" which almost works but then if I input "V1.002" (Which is likely due to the weird version numbers I am working with) I still get a match. I need to match the exact strings.

Can anyone help?

+3  A: 

Do this:

^(V1\.00|V1\.01)$

(. needs to be escaped, ^ means must be on the beginning of the text and $ must be on the end of the text)

Lucero
A: 

I would use the '^' and '$' to mark the beginning and end of the string, like this:

^(V1\.00|V1\.01)$

That way the entire string must match the regex.

Aaron
Your regex is broken, the `^` will only be applied to V1.00 and the `$` to V1.01..
Lucero
You are correct. I have corrected the regex. Thanks.
Aaron
+4  A: 

The reason you're getting a match on "V1.002" is because it is seeing the substring "V1.00", which is part of your regex. You need to specify that there is nothing more to match. So, you could do this:

^(V1\.00|V1\.01)$

A more compact way of getting the same result would be:

^(V1\.0[01])$
Alison R.
Your regex is broken, the `^` will only be applied to V1.00 and the `$` to V1.01..
Lucero
Thanks. It is fixed.
Alison R.