VMware ESXi and vSphere Cluster Management
Asterisk Dialplan Pattern Matching Examples
Learn how Asterisk dialplan patterns match dialed extensions with N, Z, X, bracket ranges, literal digits, and the dot wildcard.
An Asterisk dialplan is the call-routing logic that maps dialed extensions or patterns to actions. A dialed extension is the value selected by the caller, such as an internal number or an outside number.
Pattern matching lets one dialplan rule handle multiple possible values. An exact extension matches one specific value, while a pattern extension matches a set of values. A pattern extension always begins with an underscore character.
; Exact extension
exten => 5051,1,NoOp(Match only 5051)
; Pattern extension
exten => _NXXXXXX,1,NoOp(Match a seven-digit local number)In the first entry, 5051 is literal and matches only that extension. In the second entry, the symbols describe which characters are allowed in each position.
Asterisk Pattern Symbols
Read a pattern from left to right. Literal digits must appear exactly as written. Tokens such as N, Z, and X each consume one character. A bracket expression also consumes one character. The dot wildcard consumes one or more characters after the part that precedes it.
| Token | What It Matches | Characters Matched | Example |
|---|---|---|---|
_ | Marks the extension as a pattern | It is syntax, not a dialed character | _X. |
N | One digit from 2 through 9 | One character | N matches 2 or 8, but not 1 |
Z | One digit from 1 through 9 | One character | Z matches 1 or 9, but not 0 |
X | Any digit from 0 through 9 | One character | X matches every decimal digit |
[1-3] | One digit in the inclusive range 1 through 3 | One character | [1-3] matches 1, 2, or 3 |
. | One or more characters following the preceding pattern portion | At least one character | 5051. requires a suffix after 5051 |
| Literal digits | The exact digit shown | One character per digit | 9 matches only 9 |
How to Read Patterns from Left to Right
- Ignore the leading underscore as a marker that tells Asterisk this is a pattern.
- Identify literal digits. These create a fixed prefix or fixed portion of the value.
- Count each
N,Z,X, or bracket expression as one required character. - If the pattern ends in a dot, add at least one more character to the minimum length.
For example, _9ZXXXXXXX begins with a fixed 9. The next position is restricted by Z, and each of the seven X tokens accepts one digit. The fixed prefix narrows the possible values before the variable positions are considered.
Example: NANP-Style Seven-Digit Local Numbers
The pattern _NXXXXXX is commonly used for a North American Numbering Plan (NANP) seven-digit local-number format. NANP is a numbering system in which the first digit of a local seven-digit number is from 2 through 9.
exten => _NXXXXXX,1,NoOp(Match a seven-digit local number)N: the first digit must be 2 through 9.X X X X X X: the remaining six positions may each be any digit from 0 through 9.- The pattern requires exactly seven digits total.
| Value | Result | Reason |
|---|---|---|
2345678 | Matches | Seven digits; first digit is in the range 2-9 |
9876543 | Matches | Seven digits; first digit is in the range 2-9 |
1234567 | Does not match | First digit is 1, which N excludes |
234567 | Does not match | Only six digits |
23456789 | Does not match | Eight digits |
Example: A Prefixed Extension Pattern
An access prefix is a leading digit used to select a route, such as an outside-line prefix. The pattern _9ZXXXXXXX demonstrates a literal access prefix followed by a constrained number position.
exten => _9ZXXXXXXX,1,NoOp(Match a 9-prefixed number)- The first character must be the literal digit
9. - The second character is
Z, so it must be a digit from 1 through 9. - The remaining seven positions are
Xtokens and accept any digits from 0 through 9. - The complete value has nine digits: one
9, oneZ, and sevenXpositions.
| Value | Result | Reason |
|---|---|---|
912345678 | Matches | Starts with 9 and the second digit is 1 |
999999999 | Matches | Starts with 9 and every remaining position is valid |
901234567 | Does not match | The second digit is 0, which Z excludes |
812345678 | Does not match | The required leading 9 is missing |
Example: Fixed Prefix with a Variable Suffix
The pattern _5051. uses a literal prefix and a dot wildcard.
exten => _5051.,1,NoOp(Match values beginning with 5051)5051is a required literal prefix.- The dot requires one or more additional characters after that prefix.
- A matching value must contain at least five characters total: the four literal digits plus at least one suffix character.
| Value | Result | Reason |
|---|---|---|
50510 | Matches | Has the prefix 5051 plus one suffix digit |
5051234 | Matches | Has the prefix plus several suffix digits |
5051 | Does not match | The dot requires at least one additional character |
60510 | Does not match | The literal prefix is not present |
The dot is not an optional suffix marker. If the bare value 5051 must also be accepted, define an exact 5051 extension or create another explicit rule.
Example: Digit Range Matching
A bracket expression is a character range or set enclosed in brackets. In _7[1-3], the first digit is fixed and the second digit is selected from an inclusive range.
exten => _7[1-3],1,NoOp(Match extensions 71 through 73)7must be the first digit.[1-3]consumes exactly one second digit.- The second digit can be
1,2, or3.
The matching extensions are 71, 72, and 73. Values such as 70, 74, and 711 do not match this two-character pattern.
A bracket range is more compact than writing separate exact extensions for 71, 72, and 73. Use separate exact entries when those extensions need different routing behavior; use the range when they share the same logic.
Example: Broad Digit-Led Wildcard
The pattern _X. combines one unrestricted digit position with a dot wildcard.
exten => _X.,1,NoOp(Match a multi-character digit-led value)Xrequires the first character to be a digit from 0 through 9.- The dot requires at least one subsequent character.
- The pattern can therefore match digit strings longer than one digit.
- The pattern is broad and should be used cautiously because many digit-led values can satisfy it.
| Value | Result | Reason |
|---|---|---|
10 | Matches | One leading digit plus one following character |
2345 | Matches | Starts with a digit and has a suffix |
7 | Does not match | No character is available for the dot |
AB12 | Does not match | The first character is not a digit |
Pattern Matching Results at a Glance
| Pattern | Required Format | Matching Examples | Nonmatching Examples | Typical Use |
|---|---|---|---|---|
_NXXXXXX | Seven digits; first is 2-9 | 2345678, 9876543 | 1234567, 234567 | NANP-style local numbers |
_9ZXXXXXXX | 9, then 1-9, then seven digits | 912345678, 999999999 | 901234567, 812345678 | Access prefix followed by a constrained number |
_5051. | 5051 plus at least one character | 50510, 5051234 | 5051, 60510 | Variable values beginning with a fixed prefix |
_7[1-3] | 7 followed by exactly 1, 2, or 3 | 71, 72, 73 | 70, 74, 711 | Small range of related extensions |
_X. | One digit plus at least one following character | 10, 2345 | 7, AB12 | Broad digit-led matching; use cautiously |
Safe Dialplan Design
Broad patterns can unintentionally catch calls intended for more specific routing rules. For example, _X. can accept many values, including values that might eventually need a dedicated route. A narrowly defined prefix and fixed length usually makes the intended behavior easier to understand and test.
- Use literal digits and constrained tokens when the numbering plan permits it.
- Avoid a permissive wildcard when a smaller range or fixed length expresses the requirement.
- Place routing logic in the appropriate dialplan context so callers have only the permissions they should have.
- Test both values that should match and values that must not match.
- Start with
NoOp()while testing which pattern is selected, then add the actual call-routing applications.
Testing Examples
These entries illustrate pattern syntax without performing a call:
[local]
exten => _NXXXXXX,1,NoOp(Match a seven-digit local number)
exten => _9ZXXXXXXX,1,NoOp(Match a 9-prefixed number)
exten => _5051.,1,NoOp(Match values beginning with 5051)
exten => _7[1-3],1,NoOp(Match extensions 71 through 73)
exten => _X.,1,NoOp(Match a multi-character digit-led value)Use each pattern in an appropriate dialplan context. Send representative dial strings through that context and verify the expected NoOp() message. Include boundary cases such as a first digit of 1 for _NXXXXXX, a second digit of 0 for _9ZXXXXXXX, the bare value 5051, and the single digit 7 for _X..
Troubleshooting Common Mismatches
| Symptom | Likely Cause | Resolution |
|---|---|---|
A number beginning with 1 does not match _NXXXXXX. | N excludes 0 and 1. | Use X when 0 or 1 must be allowed, or retain N when NANP local-number rules are required. |
5051 does not match _5051.. | The trailing dot requires a character after the prefix. | Use an exact 5051 extension or another explicit pattern if the bare prefix is valid. |
| A pattern intended for 71 through 73 appears to need other extensions. | [1-3] permits only one second digit and no suffix is defined. | Add explicit digit tokens or a suffix wildcard only when longer extensions are intentionally valid. |
| A general routing rule captures more calls than expected. | A broad expression such as _X. matches many digit-led values. | Use narrower prefixes and constrained tokens, then test expected and unexpected dial strings. |
A 9-prefixed number with 0 as its second digit fails to match _9ZXXXXXXX. | Z matches only 1 through 9. | Use X in the second position if zero is valid for the numbering scheme. |
Exam-Relevant Notes
- A pattern extension begins with
_; an exact extension does not need that marker. Nmeans 2-9,Zmeans 1-9, andXmeans 0-9.- A literal digit matches exactly the digit written in that position.
- A bracket expression such as
[1-3]matches one digit from the specified range. - Each single-character token consumes one character.
- A dot matches one or more following characters, so it is not an optional suffix.
_NXXXXXXrequires seven digits and excludes a leading 0 or 1._X.is broad because it accepts any digit first and then any nonempty suffix; do not use it without considering what else it may catch.