开发者

Regular expression: <?>?=?\d{4}: what does it match?

开发者 https://www.devze.com 2023-03-13 13:31 出处:网络
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 d

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

精彩评论

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