<html>
<head>
    <base href="https://wiki.asterisk.org/wiki">
            <link rel="stylesheet" href="/wiki/s/2041/1/7/_/styles/combined.css?spaceKey=AST&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://wiki.asterisk.org/wiki/display/AST/Pattern+Matching">Pattern Matching</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://wiki.asterisk.org/wiki/display/~lmadsen">Leif Madsen</a>
    </h4>
        <div id="versionComment">
        <b>Comment:</b>
        Update dialplan and fix formatting<br />
    </div>
        <br/>
                         <h4>Changes (10)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Now let&#39;s look at a sample pattern. If you wanted to match all four-digit numbers that had the first two digits as six and four, you would create an extension that looks like: <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{noformat}exten=&gt;_64XX,1,SayDigits(${EXTEN}) <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{noformat}exten =&gt; _64XX,1,SayDigits(${EXTEN}) <br></td></tr>
            <tr><td class="diff-unchanged" >{noformat} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" ># After you&#39;ve examined all the digits, return the match that has been sorted to the top of the list. <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Let&#39;s <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">look&amp;nbsp;at</span> <span class="diff-added-words"style="background-color: #dfd;">look at</span> an example to better understand how this works. Let&#39;s assume Alice dials extension 6401, and she has the following patterns in her dialplan: <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{noformat}exten=&gt;_64XX,1,SayAlpha(A) <br>exten=&gt;_640X,1,SayAlpha(B) <br>exten=&gt;_64NX,1,SayAlpha(C) <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{noformat}exten =&gt; _64XX,1,SayAlpha(A) <br>exten =&gt; _640X,1,SayAlpha(B) <br>exten =&gt; _64NX,1,SayAlpha(C) <br>exten =&gt; _6XX1,1,SayAlpha(D){noformat} <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">exten=&gt;_6XX1,1,SayAlpha(D){noformat}</span>Can</span> you tell (without reading ahead) which one would match? <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Let&#39;s walk step by step through the rules explained above, and see what happens when Alice dials *6401*. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>To verify that Asterisk actually does sort the extensions in the manner that we&#39;ve described, add the following extensions to the *\[docs:users\]* context of your own dialplan. <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{noformat}}exten=&gt;_64XX,1,SayAlpha(A) <br>exten=&gt;_640X,1,SayAlpha(B) <br>exten=&gt;_64NX,1,SayAlpha(C) <br>exten=&gt;_6XX1,1,SayAlpha(D){noformat} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{noformat}exten =&gt; _64XX,1,SayAlpha(A) <br>exten =&gt; _640X,1,SayAlpha(B) <br>exten =&gt; _64NX,1,SayAlpha(C) <br>exten =&gt; _6XX1,1,SayAlpha(D){noformat} <br></td></tr>
            <tr><td class="diff-unchanged" >Reload the dialplan, and then type *dialplan show 6104@users* at the Asterisk CLI. Asterisk will show you what would match if you were to dial extension *6104* in the *\[docs:users\]* context. <br>{noformat}server*CLI&gt; dialplan show 6401@users <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >You can then dial extension *6104*&amp;nbsp;to try it out on your own. <br>{warning:title=Be Careful with Pattern Matching }Please be aware that because of the way auto-fallthrough works, if Asterisk can&#39;t find the next priority number for the current extension or pattern match, it will also look for that same priority in a less specific pattern match. Consider the following example: <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">{code:lang=javascript}exten=&gt;6410,1,SayDigits(987) <br>exten=&gt;_641X,1,SayDigits(12345) <br>exten=&gt;_641X,n,SayDigits(54321) <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{code:lang=javascript}exten =&gt; 6410,1,SayDigits(987) <br>exten =&gt; _641X,1,SayDigits(12345) <br>exten =&gt; _641X,n,SayDigits(54321) <br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br>If you were to dial extension 6410, you&#39;d hear &quot;nine eight seven five four three two one&quot;. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>The next concept we'll cover is called <em>pattern matching</em>. Pattern matching allows us to create extension patterns in our dialplan that match more than one possible dialed number. Pattern matching saves us from having to create an extension in the dialplan for every possible number that might be dialed.</p>

<p>When Alice dials a number on her phone, Asterisk first looks for an extension (in the context specified by the channel driver configuration) that matches exactly what Alice dialed. If there's no exact match, Asterisk then looks for a pattern match that matches. After we show the syntax and some basic examples of pattern matching, we'll explain how Asterisk finds the best match if there are two or more patterns which match the dialed number.</p>

<p>Pattern matches always begin with an underscore. This is how Asterisk recognizes that the extension is a pattern and not just an extension with a funny name. Within the pattern, we use various letter and characters to represent sets or ranges of numbers. Here are the most common letters:</p>

<h4><a name="PatternMatching-X"></a>X</h4>

<p>The letter <b>X</b>&nbsp;represents a single digit from 0 to 9.</p>

<h4><a name="PatternMatching-Z"></a>Z</h4>

<p>The letter <b>Z</b>&nbsp;represents any digit from 1 to 9.</p>

<h4><a name="PatternMatching-N"></a>N</h4>

<p>The letter <b>N</b>&nbsp;represents a single digit from 2 to 9.</p>

<p>Now let's look at a sample pattern. If you wanted to match all four-digit numbers that had the first two digits as six and four, you would create an extension that looks like:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>exten =&gt; _64XX,1,SayDigits(${EXTEN})
</pre>
</div></div>

<p>In this example, each <b>X</b> represents a single digit, with any value from zero to nine. We're essentially saying "The first digit must be a six, the second digit must be a four, the third digit can be anything from zero to nine, and the fourth digit can be anything from zero to nine".</p>

<p>If we want to be more specific about a range of numbers, we can put those numbers or number ranges in square brackets. For example, what if we wanted the second digit to be either a three or a four? One way would be to create two patterns (<b>&#95;64XX</b> and <b>&#95;63XX</b>), but a more compact method would be to do <b>&#95;6[docs:34]XX</b>. This specifies that the first digit must be a six, the second digit can be either a three or a four, and that the last two digits can be anything from zero to nine.</p>

<p>You can also use ranges within square brackets. For example, <b>[docs:1-468]</b> would match a single digit from one through four or six or eight. It does not match any number from one to four hundred sixty-eight&#33;</p>

<p>Within Asterisk patterns, we can also use a couple of other characters to represent ranges of numbers. The period character (<b>.</b>) within a pattern matches on one or more remaining digits in the pattern. It typically appears at the end of a pattern match, especially when you want to match extensions of an indeterminate length. As an example, the pattern <b>&#95;9876</b>. would match any number that began with <b>9876</b> and had at least one more character or digit.</p>

<p>The exclamation mark (<b>&#33;</b>) character is similar to the period and also matches one more more remaining characters, but is used in overlap dialing. For example, <b>&#95;9876&#33;</b> would match any number that began with <b>9876</b>, and would respond that the number was complete as soon as there was an unambiguous match.</p>
<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/wiki/images/icons/emoticons/forbidden.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Be Careful With Wildcards in Pattern Matches</b><br />Please be extremely cautious when using the period and exclamation mark characters in your pattern matches. They match more than just digits, they also match on characters as well, and if you're not careful to filter the input from your callers, a malicious caller might try to use these wildcards to bypass security boundaries on your system.

<p>For a more complete explanation of this topic and how you can protect yourself, please refer to the <b>README-SERIOUSLY.bestpractices.txt</b> file in the Asterisk source code.</p></td></tr></table></div>
<p>Now let's show what happens when there is more than one pattern that matches the dialed number. How does Asterisk know which pattern to choose as the best match?</p>

<p>Asterisk uses a simple set of rules to determine the best match. They are:</p>

<ol>
        <li>Examine the first digit eliminate any patterns which don't match the first digit of the dialed number</li>
        <li>Sort the remaining patterns based on the most constrained match for the current digit. By most constrained, we mean the pattern that has the fewest possible matches for this digit. As an example, the <b>N</b> character has 8 possible matches (two through nine), while an <b>X</b> has ten possible matches.</li>
        <li>In the case of a match, sort the patterns in ASCII sort order. For example, <b>&#95;[docs:234]X</b> and <b>&#95;[docs:345]X</b> have three possible matches in the first digit, but <b>234</b> comes before <b>345</b> in ASCII sort order.</li>
        <li>Move on to the next digit (moving digit by digit from left to right), and eliminate any patterns which don't match the current digit of the dialed number. Then continue back at step number two.</li>
        <li>After you've examined all the digits, return the match that has been sorted to the top of the list.</li>
</ol>


<p>Let's look at an example to better understand how this works. Let's assume Alice dials extension 6401, and she has the following patterns in her dialplan:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>exten =&gt; _64XX,1,SayAlpha(A)
exten =&gt; _640X,1,SayAlpha(B)
exten =&gt; _64NX,1,SayAlpha(C)
exten =&gt; _6XX1,1,SayAlpha(D)</pre>
</div></div>
<p>Can you tell (without reading ahead) which one would match?</p>

<p>Let's walk step by step through the rules explained above, and see what happens when Alice dials <b>6401</b>.</p>

<h5><a name="PatternMatching-Rule1"></a>Rule 1</h5>

<p>We look at the first digit, and all the patterns match.</p>

<h5><a name="PatternMatching-Rule2"></a>Rule 2</h5>

<p>Each of the patterns have the same number of possible matches on this digit (one match &#8212; the number six).</p>

<h5><a name="PatternMatching-Rule3"></a>Rule 3</h5>

<p>We sort the patterns in ASCII sort order.</p>

<h5><a name="PatternMatching-Rule4"></a>Rule 4</h5>

<p>We move on to the second digit. There are no patterns that can be eliminated based on the second digit, so we go back to rule two for this digit.</p>

<h5><a name="PatternMatching-Rule2"></a>Rule 2</h5>

<p>The three patterns with a <b>4</b> in the second digit are more constrained than the <b>X</b>, so they get sorted to the top.</p>

<h5><a name="PatternMatching-Rule3"></a>Rule 3</h5>

<p>The top three patterns get sorted in ASCII sort order, since they are tied in the number of possible matches.</p>

<h5><a name="PatternMatching-Rule4"></a>Rule 4</h5>

<p>We move on to the third digit. The third pattern (the one that would call <b>SayAlpha(C)</b>) is eliminated, because the third digit of this pattern (the <b>N</b>) doesn't match the third dialed digit (the <b>0</b>). The other patterns match, so we now go back to rule two.</p>

<h5><a name="PatternMatching-Rule2"></a>Rule 2</h5>

<p>The second pattern (the one that would call <b>SayAlpha(B)</b>) is the most constrained, as it only has a single possibility, so it gets sorted to the top.</p>

<h5><a name="PatternMatching-Rule3"></a>Rule 3</h5>

<p>There are no ties at the top of the sorting table, so we can move on to rule four.</p>

<h5><a name="PatternMatching-Rule4"></a>Rule 4</h5>

<p>We move on to the fourth digit. Since all the remaining patterns match, the second pattern remains at the top of the sorting table. You might be asking yourself... "What about the fourth pattern? Isn't it more constrained?" Remember that it was less constrained in an earlier digit, so it would only match if none of the other patterns above it in the sorting table matched on this digit.</p>

<h5><a name="PatternMatching-Step5"></a>Step 5</h5>

<p>Since we have run out of digits, we know that Asterisk will match on the second pattern, as it is the one at the top of the sorting table.</p>

<p>To verify that Asterisk actually does sort the extensions in the manner that we've described, add the following extensions to the <b>[docs:users]</b> context of your own dialplan.</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>exten =&gt; _64XX,1,SayAlpha(A)
exten =&gt; _640X,1,SayAlpha(B)
exten =&gt; _64NX,1,SayAlpha(C)
exten =&gt; _6XX1,1,SayAlpha(D)</pre>
</div></div>
<p>Reload the dialplan, and then type <b>dialplan show 6104@users</b> at the Asterisk CLI. Asterisk will show you what would match if you were to dial extension <b>6104</b> in the <b>[docs:users]</b> context.</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent panelContent">
<pre>server*CLI&gt; dialplan show 6401@users
[ Context 'users' created by 'pbx_config' ]
  '_640X' =&gt;        1. SayAlpha(B)                                [pbx_config]
  '_64XX' =&gt;        1. SayAlpha(A)                                [pbx_config]
  '_6XX1' =&gt;        1. SayAlpha(D)                                [pbx_config]

-= 3 extensions (3 priorities) in 1 context. =-</pre>
</div></div>
<p>You can then dial extension <b>6104</b>&nbsp;to try it out on your own.</p>
<div class='panelMacro'><table class='warningMacro'><colgroup><col width='24'><col></colgroup><tr><td valign='top'><img src="/wiki/images/icons/emoticons/forbidden.gif" width="16" height="16" align="absmiddle" alt="" border="0"></td><td><b>Be Careful with Pattern Matching</b><br />Please be aware that because of the way auto-fallthrough works, if Asterisk can't find the next priority number for the current extension or pattern match, it will also look for that same priority in a less specific pattern match. Consider the following example:
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: Confluence; brush: javascript; gutter: false"><![CDATA[exten =&gt; 6410,1,SayDigits(987)
exten =&gt; _641X,1,SayDigits(12345)
exten =&gt; _641X,n,SayDigits(54321)
]]></script>
</div></div>
<p>If you were to dial extension 6410, you'd hear "nine eight seven five four three two one".</p>

<p>We strongly recommend you make the <b>Hangup()</b> application be the last priority of any extension to avoid this problem, unless you purposely want to fall through to a less specific match.</p></td></tr></table></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://wiki.asterisk.org/wiki/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://wiki.asterisk.org/wiki/display/AST/Pattern+Matching">View Online</a>
        |
        <a href="https://wiki.asterisk.org/wiki/pages/diffpagesbyversion.action?pageId=4817353&revisedVersion=11&originalVersion=10">View Changes</a>
                |
        <a href="https://wiki.asterisk.org/wiki/display/AST/Pattern+Matching?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>