问题
I'm trying to use a regular expression in C# to match a software version number that can contain:
- a 2 digit number
- a 1 or 2 digit number (not starting in 0)
- another 1 or 2 digit number (not starting in 0)
- a 1, 2, 3, 4 or 5 digit number (not starting in 0)
- an option letter at the end enclosed in square brackets.
Some examples:
10.1.23.26812 83.33.7.5 10.1.23.26812[d] 83.33.7.5[q]
Invalid examples:
10.1.23.26812[ 83.33.7.5] 10.1.23.26812[d 83.33.7.5q
I have tried the following:
string rex = @"[0-9][0-9][.][1-9]([0-9])?[.][1-9]([0-9])?[.][1-9]([0-9])?([0-9])?([0-9])?([0-9])?([[][a-zA-Z][]])?";
(note: if I try without the "@" and just escape the square brackets by doing "\[" I get an error saying "Unrecognised escape sequence")
I can get to the point where the version number is validating correctly, but it accepts anything that comes after (for example: "10.1.23.26812thisShouldBeWrong" is being matched as correct).
So my question is: is there a way of using a regular expression to match / check for square brackets in a string or would I need to convert it to a different character (eg: change [a] to a and match for *s instead)?
回答1:
This happens because the regex matches part of the string, and you haven't told it to force the entire string to match. Also, you can simplify your regex a lot (for example, you don't need all those capturing groups:
string rex = @"^[0-9]{2}\.[1-9][0-9]?\.[1-9][0-9]?\.[1-9][0-9]{0,4}(?:\[[a-zA-Z]\])?$";
The ^
and $
are anchors that match the start and end of the string.
The error message you mentioned has to do with the fact that you need to escape the backslash, too, if you don't use a verbatim string. So a literal opening bracket can be matched in a regex as "[[]"
or "\\["
or @"\["
. The latter form is preferred.
回答2:
You need to anchor the regex with ^
and $
string rex = @"^[0-9][0-9][.][1-9]([0-9])?[.][1-9]([0-9])?[.][1-9]([0-9])?([0-9])?([0-9])?([0-9])?([[][a-zA-Z][]])?$";
the reason the 10.1.23.26812thisShouldBeWrong
matches is because it matches the substring 10.1.23.26812
The regex can be simplfied slightly for readability
string rex = @"^\d{2}\.([1-9]\d?\.){2}[1-9]\d{0,4}(\[[a-zA-Z]\])?$";
In response to TimCross warning - updated regex
string rex = @"^[0-9]{2}\.([1-9][0-9]?\.){2}[1-9][0-9]{0,4}(\[[a-zA-Z]\])?$";
来源:https://stackoverflow.com/questions/17231637/c-sharp-regular-expression-to-match-square-brackets