Regular Expressions Tutorial for FreeSWITCH - Omid-Mohajerani/freeswitch GitHub Wiki

Regular Expressions are "formulas" used to describe a string of text. Regular expressions (regex, regexes) are at the heart of dialplan, and are used in many other parts of FreeSWITCH configuration. You can describe patterns of extensions, DIDs, users, callers, callees, gateways, etc with regular expressions in FreeSWITCH.

In dialplan regexes are used to define the "expression" criterion of the "condition" tag.

  <extension name="Talking Clock Time" ><!--e.g. 10:56pm-->
    <condition field="destination_number" expression="^9170$">
      <action application="answer"/>
      <action application="sleep" data="1000"/>
      <action application="say" data="en CURRENT_TIME pronounced
                                      ${strepoch()}"/>
      <action application="hangup"/>
    </condition>
  </extension>
Character Matches
^ asserts position at start of a line
$ asserts position at the end of a line
\d matches a digit (equivalent to [0-9])
\D matches any character that's not a digit (equivalent to [^0-9])
. matches any character (except for line terminators)
(xyz|ABC) xyz or ABC
[xyz] xyz matches a single character in the list xyz (case sensitive)
[0-9] 0 to 9
{m} matches the previous token exactly m times
{m,n} matches the previous token between m and n times
* matches the previous token between zero and unlimited times
+ matches the previous token between one and unlimited times
? matches the previous token between zero and one times (Optional Character)

Examples:

example description Example
^\d\D{3}$ ^ asserts position at start of a line
\d matches a digit (equivalent to [0-9]
\D matches any character that's not a digit (equivalent to [^0-9])
{3} matches the previous token exactly 3 times
$ asserts position at the end of a line
1ABC
example description Example
^601\d{8,9}$ ^ ^ asserts position at start of a line
601 matches the characters 601 literally (case sensitive)
\d matches a digit (equivalent to [0-9])
{8,9} matches the previous token between 8 and 9 times
$ asserts position at the end of a line
60171234567
601265483423

You can check regular expressions online here: https://regex101.com/

The regex FreeSWITCH console command needs at least two arguments: the data to test and the pattern to match against. The arguments are separated by a | (pipe) character. The regex command will return true if the data and the pattern match, otherwise it will return false.

Reference:

https://freeswitch.org/confluence/display/FREESWITCH/Regular+Expression

⚠️ **GitHub.com Fallback** ⚠️