Regex Example: International Number Matching

Last Updated : Oct 16, 2023 |

The example scenario uses a regex string to match the dialing of international calls. The string matches calls prefixed with either + or 00 (the UK dialing prefix for international calls).

The regex string used was: ^(\+|00)(.+)$

The string works as follows:

  1. The ^ matches the start of the string.

  2. The ( ) brackets group elements for processing and, potentially for future digit translations.

  3. The first pair of brackets contain \+|00:

    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 | separates different possible matches.

    3. Therefore, the first pair of brackets matches any numbers beginning with a + or 00.

  4. The second pair of brackets contains .+:

    1. The . matches any digit or character.

    2. The + modifies the preceding . to match any number of matches.

    3. Therefore, the .+ operate together to match any number of digits and characters.

  5. The second pair of brackets enclose the match for any number of characters. In this example, a translation can use the value $2 to refer to the matched characters, without their + or 00 prefix.

  6. The $ matches the end of the string.