TimeToCode TimeToCode - 8 months ago 42
C# Question

Small trouble with a regular expression

I need to build a regular expression, with the following details. The regular expression needs to match in the following chars structure:


  1. OT
    : They may be uppercase or lowercase, but I only need to match if it is
    followed by
    , not
    and followed by

  2. -
    Followed by this character.

  3. 001
    Followed by 3 or more numbers.

  4. -
    Followed by this character.

  5. 16
    And finally followed by exact 2 numbers.

This is what I've tried:


This regular expression works fine, except if I write
, the problem is with the
chars, I need to match only if a
is followed by

Thanks and any question post on comments.

Answer Source

by using [OT|ot]{2} you are actually comparing a string that has char O or o and T or t of length 2. It is like using something like this.

[ABCD]{2} : where it matches all the possibles combinations of characters.

To solve this, use (OT|ot) which matches between this particular pattern OT or ot.