TimeToCode TimeToCode - 10 days ago 5
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:

OT-001-16




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

  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:

/([OT|ot]{2})-(\d{3,})-(\d{2})/g


This regular expression works fine, except if I write
TO-0052-54
, the problem is with the
OT
chars, I need to match only if a
O
is followed by
T
.

Thanks and any question post on comments.

Answer

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.

Comments