Regular Expression to List accepted words
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 开发者_如何学JAVAstrings.
Can anyone help?
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])$
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)
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.
精彩评论