开发者

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:

  1. ''
  2. '<'
  3. '>'
  4. '='
  5. '<>'
  6. '<='
  7. '>='
  8. '<>='


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜