Regex Example: Extension Number Matching

Last Updated : Oct 16, 2023 |

The example scenario uses a regex string to match any 3-digit number that begins with a 2. It does that to match any internal extension numbers on the IP Office system.

The regex string used was: ^\+?(2\d{2})$

The string works as follows:

  1. The ^ matches the start of the string.

  2. The \+? is an optional match:

    1. The \+ matches a literal + character (the E.164 international prefix indicator). The string needs a \ as a + on its own is a regex command.

    2. The ? modifies the preceding \+ to mean match if the + is present or not.

  3. The ( ) brackets group elements.

  4. The brackets contain 2\d{2}:

    1. The 2 matches a 2 at the start of the number.

    2. The \d matches any digit. The string needs a \ as a d is the regex to match an alphabetic d.

    3. The {2} modifies the preceding \d to match 2 instances of that element.

    4. Therefore, \d{2} matches any two digits.

  5. The $ matches the end of the string. Including this means the pattern will not match longer numbers that begin with a 2.