# About

A list of useful regular expression examples.Regular Expressions are used throughout FreeSWITCH. See Dialplan_XML for complete examples of the regular expressions used in conditional statements in the dialplan.

# Capturing Values

Matching items in parentheses are captured in variables sequentially named $1, $2, etc. $0 contains the entire pattern.

# Examples

## Any toll free Number

Matches any toll free number

Example: **8005551212** or **18005551212** or **+18005551212** will match and in each case $2 is populated with "8005551212"

Explanation:

- ^ - indicates the beginning of a regular expression (required);
- ( - indicates the beginning of a regular expression block - blocks are important to define inner expressions so they can be referenced by the variables $1, $2, $3, etc;
- \+1|1? - indicates optional digits '+1' or '1' (the ? sign defines the literal as optional);
- ) - closes the block;
- 8 - matches the literal '8';
- ( - another block starting;
- 00|55|66|77|88 - matches 00 or 55 or 66 or 77 or 88 (you guessed right, the | sign is the OR operator);
- ) - closes the inner block;
- [2-9] - matches one digit in the range from 2 to 9 (2, 3, 4, 5, 6, 7, 8 and 9), and as you also guessed the [] pair of brackets encloses a range; other range examples: [0-9] matches 0 to 9; [a-z] matches a, b, c, ...z);
- \d - matches any valid digit (same as [0-9]);
- {6} - defines the number of occurrences for the previous expression, i.e. exactly 6 digits in the range 0-9. This can also contain a variable number of occurrences, for example to match a sequence of 6 to 9 digits: {6,9}; or to match at least 6 with no maximum: {6,};
- ) - closes the other block;
- $ - indicates the end of the regular expression (required);

## Any string of exactly seven digits

Matches any string of exactly seven digits

## Any string of exactly 10 digits

Matches any string of exactly 10 digits

## Any string of exactly 11 digits

Matches any string of exactly 11 digits

## A string with prefix of 9 and an additional 11 digits

Matches a string with a prefix of 9 and an additional 11 digits.

## Any string of exactly 3 or 4 digits

Matches any string of exactly 3 or 4 digits

## Any string from 3 to 5 digits

Matches any string from 3 to 5 digits

## Variable number of digits

Matches a variable number of digits after a prefix of '9'

## International country codes

## NANPA +1NxxNxxXXXX E.164 Dialstring

## LNP (Local Number Portability)

Example:

Example:

## Clustering vs Capturing

Sometimes, you need to use parentheses to group a set of choices, but you are not interested in saving what is matched. Place a question mark and colon after the opening parenthesis to accomplish this. The example below matches a leading "+" or leading "00", but the matched information is not saved. The second set of parentheses does save information that is matched.

The following example matches a number and saves the information in two pieces as leading characters ($1) and the telephone number ($2).

Parentheses can be nested. When trying to determine which variable holds the matched information, count the opening parentheses from left to right. The first opening parenthesis will store information in $1, the second opening parenthesis will store information in $2 and so on.

In the following example, the first set of parentheses does not store any information.

## Greediness

If you do .*? it will be un-greedy (or use \d to only match the numbers if you know it will be a number)

## Case Insensitive

To make a match case insensitive, prepend (?i) to your match string.

# Further Reading

Regular Expressions are serious business. They are quite useful in computing in general. Here are some other resources for learning more:

http://regexlib.com/Default.aspx

http://www.regular-expressions.info/

http://www.weitz.de/regex-coach/

http://regexlib.com/CheatSheet.aspx

http://www.zytrax.com/tech/web/regex.htm

http://oreilly.com/catalog/9781565922570/

http://perldoc.perl.org/perlreref.html

# Test Your Regex

## Use fs_cli

See: mod_commands#regex

## Online Tools

## Simple Perl Script

You can also use the following perl script to test your regular expression, or make some adjustment of regex based on what you need.

```
Result:
```

# See Also

regex API command performs pattern matching on the fs_cli

Regular Expression Cheat Sheet: http://overapi.com/regex/