
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;
 \+11?  indicates optional digits '+1' or '1' (the ? sign defines the literal as optional);
 )  closes the block;
 8  matches the literal '8';
 (  another block starting;
 0055667788  matches 00 or 55 or 66 or 77 or 88 (you guessed right, the  sign is the OR operator);
 )  closes the inner block;
 [29]  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: [09] matches 0 to 9; [az] matches a, b, c, ...z);
 \d  matches any valid digit (same as [09]);
 {6}  defines the number of occurrences for the previous expression, i.e. exactly 6 digits in the range 09. 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 ungreedy (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.regularexpressions.info/
http://www.weitz.de/regexcoach/
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/