Regular expression: <?>?=?\d{4}: what does it match?
In a C# class, I came across this开发者_高级运维 regular expression:
<?>?=?\d{4}
It is pretty obvious that its last part (\d{4}
) matches 4 decimal digits but what about <?>?=?
? What does it match?
Thanks for any explanations.
Four digits at the end preceded by the <
, >
and =
occurring zero or once in that order.
Match:
<>=1234
>=1234
=1234
1234
<=1234
The expression '<?>?=?'
matches a '<' char (or none) possibly followed by a '>' possibly followed by a '='. Thus all of the following will match:
- ''
- '<'
- '>'
- '='
- '<>'
- '<='
- '>='
- '<>='
The question mark after the characters make it optional, so it matches any combination where each character can be present or not:
- <>=
- <>
- <=
- <
- >=
- >
- =
It's probably meant to match any of the three characters on its own, but then you would rather use [<>=]?
instead.
精彩评论