Skip to content

Asterisk Dialplan Pattern Matching and Wildcards Explained

Learn how Asterisk dialplan pattern matching works: X, Z, N, brackets, the . and ! wildcards, sort order, and how to test which pattern wins.

By · Updated · Published

Cables - The Missing Link - Desktop Audio Upgrade Part 2
Photo by Jordanhill School D&T Dept · CC BY 2.0 · via Flickr

How does Asterisk dialplan pattern matching work?

An extension that starts with an underscore is a pattern, so one line can match many dialed numbers. Asterisk tries exact extensions first and uses patterns only when no exact extension fits.

Every pattern begins with an underscore. Without it, the name is a literal extension, and Asterisk matches it only when the dialed digits are exactly the same. If a pattern seems to do nothing, check for the missing underscore first. The Asterisk pattern matching documentation lists each pattern character.

Here is a short example. In extensions.conf:

INI config
[users]
exten => _64XX,1,NoOp(Matched ${EXTEN})
exten => _64XX,2,Hangup()

Each line has three parts separated by commas: the extension, the priority and the application. The first line matches any dialed number from 6400 to 6499, because each X is one digit. NoOp() does nothing to the call, but the docs say it is useful for debugging purposes. ${EXTEN} holds the dialed number. The second line ends the call with Hangup(), which hangs up the calling channel.

What do X, Z, N and brackets mean in an Asterisk pattern?

We compiled this reference table from the official Asterisk docs on November 14, 2026. Nothing in it was tested. Each row comes from the pattern matching page.

CharacterWhat it matchesExample
_Marks the name as a pattern_64XX
XOne digit, 0 to 9_64XX matches 6400 to 6499
ZOne digit, 1 to 9_ZXX: the first digit cannot be 0
NOne digit, 2 to 9_NXX: the first digit cannot be 0 or 1
[ ]One character from a set_6[34]XX matches 63XX and 64XX
.One or more remaining characters_9876. needs at least one more character
!Zero or more remaining characters_9876! also matches 9876
-Ignored, except as a range inside a set[1-4] matches one digit from 1 to 4

X, Z and N are shorthand for a single digit within a range. X matches one digit from 0 to 9, so it accepts any digit. Z matches one digit from 1 to 9, so it never accepts zero. N matches one digit from 2 to 9, so it rejects both zero and one.

Brackets define a character set. A set matches one character, and you can list single digits or ranges inside it. The example _6[34]XX allows a 3 or a 4 as the second digit. The two X characters at the end accept any digit.

Inside a set, X, N and Z have no special meaning. Only the dash, which makes a range, and the backslash, which escapes a character, still do something. This is easy to misread. The set [1-468] matches one digit: 1, 2, 3, 4, 6 or 8. It does not match every number from 1 to 468, even though it looks like a range.

To match a literal period or exclamation mark, put it in a set, such as [.] or [!]. A set holds the character as a single literal, so Asterisk does not treat it as a wildcard.

What is the difference between the . and ! wildcards?

Both wildcards sit at the end of a pattern. The period matches one or more remaining characters. The exclamation mark matches zero or more remaining characters. That difference changes which numbers match.

_9876. needs at least one more character after 9876, so the bare number 9876 does not match it. _9876! also matches 9876 itself, because it allows nothing after those digits.

The docs say the exclamation mark is used in overlap dialing to dial through Asterisk. The advanced examples show one way to use it.

Both wildcards match almost any input, so the docs warn to be extremely cautious with them. Unfiltered caller input could use them to bypass security boundaries. Filter what callers can enter before a wildcard sees it, and keep wildcards to the routes that need them.

Asterisk treats a period or an exclamation mark as the end of a pattern. Put the wildcard last, and use a set when you need either character as a literal. The fall-through section below shows what happens when a broad catch-all such as _. takes calls it was never meant to take.

Which pattern wins when several patterns match?

Asterisk sorts every matching extension and uses the first one in the sorted list. The documented sort order works like this:

  • The dash has no effect in matching or sorting extensions, except as a range inside a set.
  • Non-pattern extensions sort in ASCII order before any pattern.
  • Patterns sort by the most constrained character set per digit first.
  • Character sets with the same number of characters sort in ASCII order.
  • The period wildcard sorts after character sets.
  • The exclamation mark wildcard sorts after the period wildcard.

The rule about non-pattern extensions means an exact extension is tried before any pattern that also matches it. The rule about constrained sets explains why N beats X. N accepts eight digits, from 2 to 9, while X accepts ten, from 0 to 9. The narrower set sorts first.

The docs give a worked example with the dialed number 6421. They sort the patterns as _640X, _64NX, _64XX, _6[34]NX, _6[45]NX, _6XX1 and _6. The first entry, _640X, does not match 6421, because its third digit must be 0. The next entry, _64NX, does match, so the docs say _64NX wins.

When several patterns share a prefix, check the result with dialplan show before you rely on the order. The next section shows the command.

How do I see which extension Asterisk will match?

Photo of computer, pc, workplace
Photo by janeb13 · Pixabay Content License · via Pixabay

Run this command at the Asterisk CLI to list the extensions that match a number:

Output
dialplan show 6421@users

The command lists every extension in the [users] context that matches 6421. The part after the at sign names the context. To see the whole context, run dialplan show users at the same prompt.

Use this check after each change to a pattern. If a catch-all such as _. appears in the list, you know it also matches the number, even if you did not mean it to.

How do I strip a prefix from ${EXTEN} or match on caller ID?

Woman's hand holding and touching smartphone screen of a smartphone
Photo by Detail .co · Unsplash License · via Unsplash

The dialed number is in ${EXTEN}. You can read it in the dialplan, but the docs say it is read-only there. Substring syntax lets you take part of a variable, and the form is ${variable:skip:length}.

The skip field tells Asterisk how many digits to strip off the front of the value. A negative skip does the reverse, and returns the specified number of digits from the end. The optional length field returns at most that many digits. The table below uses the value 98765 for the variable NUMBER.

ExpressionResult
${NUMBER:2}765
${NUMBER:-2}65
${NUMBER:0:3}987
${NUMBER:1:3}876

Each row shows one part of the syntax. The first row skips two digits from the front, which leaves 765. The second row uses a negative skip, which returns the last two digits, 65. The third row keeps at most three digits from the front, which gives 987. The fourth row skips one digit and keeps the next three, which gives 876.

To drop a dialing prefix, such as the 9 used for an outside line, use a skip of one. For example, ${EXTEN:1} removes the first digit of the dialed number and keeps the rest. The first advanced example uses this form.

To match on caller ID, add a forward slash after the dialed extension or pattern, followed by the caller ID pattern to match. This lets one dialed number take different routes depending on who calls. In extensions.conf:

INI config
[users]
exten => 6421/_64XX,1,NoOp(Dialed 6421 from a caller ID that matches _64XX)
exten => 6421/_64XX,2,Hangup()

The first line handles a call dialed to 6421 only when the caller ID matches _64XX. The second line ends that route with Hangup(). The docs warn that changing CALLERID(num) inside an extension matched by caller ID can immediately send the call to another handler. CALLERID() gets or sets caller ID data on the channel, and num is one of its datatypes.

Why does my call fall through to a different pattern?

Asterisk uses auto-fallthrough. If the matched extension does not have the next priority, Asterisk looks for that priority in a less specific pattern.

The pattern matching documentation strongly recommends making Hangup() the last priority of every extension. A call that reaches Hangup() ends there, so it does not slide into a less specific pattern.

Included contexts are also searched when the current context has no match. Asterisk does not stop after the first match, so avoid overlapping patterns and duplicate extensions across included contexts. The include statements documentation shows a _. catch-all in an included context taking over a call to an extension it was not meant for.

If a call does something you did not expect, run dialplan show with the dialed number and context, as shown in the section above, before you change anything else.

Advanced examples

The three examples below combine the pieces above. We did not test them. Each one uses only syntax from the Asterisk documentation. The PJSIP/alice endpoint and the numbers are placeholders, not real settings.

The first example: an outbound route that drops the leading 9. The period wildcard needs at least one digit after the 9, so a bare 9 does not match this route. ${EXTEN:1} returns everything after the first digit, which removes the 9. The Dial line is a placeholder, so replace it with your own route. The docs examples use the form Dial(PJSIP/alice,30), where 30 is the timeout.

INI config
; Example only: strip the leading 9 from the dialed number.
[outbound]
exten => _9.,1,NoOp(Number without the 9: ${EXTEN:1})
exten => _9.,2,Dial(PJSIP/alice,30)
exten => _9.,3,Hangup()

The NoOp line adds a debugging note that contains the number without the leading 9. The Dial line places the call. The Hangup line ends the route, which keeps the call from falling through to a less specific pattern.

The second example: overlap dialing with the exclamation mark. The pattern _6XX! matches 6XX with nothing after it, as well as longer numbers that start the same way. The docs say the exclamation mark is used in overlap dialing, and this example follows that use.

INI config
; Example only: match 6XX and anything that follows it.
[overlap]
exten => _6XX!,1,NoOp(Overlap match: ${EXTEN})
exten => _6XX!,2,Dial(PJSIP/alice,30)
exten => _6XX!,3,Hangup()

The NoOp line contains the dialed number. Because the exclamation mark can match nothing, the bare 6XX matches this pattern too. This example does not call Incomplete(). The Incomplete documentation describes it as a way to signal that the matched extension is incomplete, which can be used within a pattern match that needs a longer match. Test this on a lab system before you use it in production.

The third example: caller ID routing with special extensions. The special extensions documentation says the i extension receives calls when no extension matches the digits dialed during Background() or WaitExten(). The t extension handles a response timeout in the same applications, and the h extension runs when a call is hung up. The s extension is not a catch-all, so this example does not use it.

INI config
; Example only: caller ID routing with special extensions.
[users]
exten => 6421/_64XX,1,NoOp(Dialed 6421 from a caller ID that matches _64XX)
exten => 6421/_64XX,2,Hangup()
exten => i,1,NoOp(No matching extension)
exten => i,2,Hangup()
exten => t,1,Hangup()
exten => h,1,NoOp(Call ended)

The first two lines route a call dialed to 6421 when the caller ID matches _64XX. The next two lines handle dialed digits that match nothing in the context. The t line ends a call that times out during Background() or WaitExten(). The h line runs when the call is hung up and adds a debugging note.

More guides in Open-Source PBX and SIP Servers.

Frequently asked questions

Why is _. considered a dangerous catch-all pattern?
The period wildcard matches one or more characters, so _. matches any dialed input with at least one character. The docs say to be extremely cautious with the period and exclamation mark, because unfiltered caller input could use them to bypass security boundaries.
Is the s extension a catch-all?
No. The s extension is where analog calls and macros start, and the docs say it is not a catch-all extension. The e extension acts as a catch-all for missing i, t or T extensions.

Sources

  1. Every pattern extension in the Asterisk dialplan starts with an underscore. (docs.asterisk.org)
  2. NoOp([text]) does nothing but is useful for debugging. (docs.asterisk.org)
  3. Hangup([causecode]) hangs up the calling channel. (docs.asterisk.org)
  4. Substring syntax is ${variable:skip:length}; skip strips digits from the front, so with NU (docs.asterisk.org)
  5. CALLERID(datatype,CID) gets or sets caller ID data; num is one of its datatypes. (docs.asterisk.org)
  6. include => context makes Asterisk also look in the included context when the current conte (docs.asterisk.org)
  7. Incomplete([n]) tells the PBX the matched extension is incomplete and more input should be (docs.asterisk.org)
  8. The s extension is where analog calls and macros start; it is not a catch-all. (docs.asterisk.org)