Skip to main content

Sample Regexes

Click here to expand Table of Contents

<!-- Simple regex examples for FS Conf Call -->
<include>

<!-- traditional OR logic -->
<extension name="example_01">
<condition field="destination_number" expression="^(this|that|the_other)$>
<action application="log" data="Someone dialed $1"/>
<anti-action application="log" data="Something else was dialed"/>
</condition>
</extension>

<!-- what if you wanted multiple fields in the OR logic?

IF ( caller_id_name == "Michael Collins" OR caller_id_number == 1001 )
THEN { do this thing }
ELSE { do that other thing }

Use the new 'regex' tag
-->
<extension name="example_02">
<condition regex="any" continue="true">
<!-- If either of these is true then the subsequent actions are added to execute list -->
<regex field="caller_id_name" expression="Some User"/>
<regex field="caller_id_number" expression="^1001$"/>
<action application="log" data="INFO At least one of the conditions matched!"/>
<!-- If *none* of the regexes is true then the anti-actions are added to the execute list -->
<anti-action application="log" data="WARNING None of the conditions matched!"/>>
</condition>
</extension>

<!-- a slightly more involved example -->
<extension name="example_03" continue="true">
<condition regex="any" break="never">
<regex field="caller_id_name" expression="^Michael\s*S?\s*Collins"/>
<regex field="caller_id_number" expression="^1001|3757|2816$"/>
<action application="set" data="calling_user=mercutioviz" inline="true"/>
<anti-action application="set" data="calling_user=loser" inline="true"/>
</condition>

<condition>
<action application="answer"/>
<action application="sleep" data="500"/>
<action application="playback" data="ivr/ivr-welcome_to_freeswitch.wav"/>
<action application="sleep" data="500"/>
</condition>

<condition field="${calling_user}" expression="^loser$">
<action application="playback" data="ivr/ivr-dude_you_suck.wav"/>
<anti-action application="playback" data="ivr/ivr-dude_you_rock.wav"/>
</condition>
</extension>
<!-- Just for kicks, an XOR example -->
<extension name="example_04" continue="true">
<condition regex="xor">
<!-- If only one of these is true then the subsequent actions are added to execute list -->
<regex field="caller_id_name" expression="Some User"/>
<regex field="caller_id_number" expression="^1001$"/>
<action application="log" data="INFO Only one of the conditions matched!"/>
<!-- If *none* of the regexes is true then the anti-actions are added to the execute list -->
<anti-action application="log" data="WARNING None of the conditions matched!"/>
</condition>
</extension>

<!-- Using larger AND structures
bearing in mind that the dialplan executes routing logic;
it is not a procedural scripting language
IF (cond1 AND cond2 AND cond3) THEN
do actions
ELSE
do other actions
ENDIF
-->
<extension name="example_05">
<condition regex="all">
<regex field="${sip_gateway}" expression="^${default_provider}$"/>
<regex field="${emergency_call}" expression="^true$"/>
<regex field="${db(select/emergency/autoanswer)}" expression="^1$"/>

<!-- the following actions get executed if all regexes PASS -->
<action application="set" data="call_timeout=60"/>
<action application="set" data="effective_caller_id_name=${regex(${caller_id_name}|^Emerg(_.*)$|Auto%1)}"/>
<action application="set" data="autoanswered=true"/>
<action application="bridge" data="user/1000@${domain_name},sofia/gateway/1006_7217/${mobile_number}"/>

<!-- the following anti-actions are executed if any of the regexes FAIL -->
<anti-action application="set" data="effective_caller_id_name=${regex(${caller_id_name}|^Emerg(_.*)$|NotAuto%1)}"/>
<anti-action application="set" data="call_timeout=30"/>
<anti-action application="set" data="autoanswered=false"/>
<anti-action application="bridge" data="user/1000@${domain_name},sofia/gateway/1006_7217/${mobile_number}"/>
</condition>
</extension>


<!-- empty extension markup; C&P to your heart's content -->
<extension name="example_##">
<condition>
<action/>
<anti-action/>
</condition>
</extension>

</include>