Vitali Climenco
Vitali Climenco

Reputation: 1314

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.

Upvotes: 0

Views: 873

Answers (3)

Guffa
Guffa

Reputation: 700252

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.

Upvotes: 2

manojlds
manojlds

Reputation: 301117

Four digits at the end preceded by the < , > and = occurring zero or once in that order.

Match:

<>=1234
>=1234
=1234
1234
<=1234

Upvotes: 4

Howard
Howard

Reputation: 39197

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. '<>='

Upvotes: 3

Related Questions