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.

TokenWhat It MatchesCharacters MatchedExample
_Marks the extension as a patternIt is syntax, not a dialed character_X.
NOne digit from 2 through 9One characterN matches 2 or 8, but not 1
ZOne digit from 1 through 9One characterZ matches 1 or 9, but not 0
XAny digit from 0 through 9One characterX matches every decimal digit
[1-3]One digit in the inclusive range 1 through 3One character[1-3] matches 1, 2, or 3
.One or more characters following the preceding pattern portionAt least one character5051. requires a suffix after 5051
Literal digitsThe exact digit shownOne character per digit9 matches only 9

How to Read Patterns from Left to Right

  1. Ignore the leading underscore as a marker that tells Asterisk this is a pattern.
  2. Identify literal digits. These create a fixed prefix or fixed portion of the value.
  3. Count each N, Z, X, or bracket expression as one required character.
  4. 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.
ValueResultReason
2345678MatchesSeven digits; first digit is in the range 2-9
9876543MatchesSeven digits; first digit is in the range 2-9
1234567Does not matchFirst digit is 1, which N excludes
234567Does not matchOnly six digits
23456789Does not matchEight 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 X tokens and accept any digits from 0 through 9.
  • The complete value has nine digits: one 9, one Z, and seven X positions.
ValueResultReason
912345678MatchesStarts with 9 and the second digit is 1
999999999MatchesStarts with 9 and every remaining position is valid
901234567Does not matchThe second digit is 0, which Z excludes
812345678Does not matchThe 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)
  • 5051 is 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.
ValueResultReason
50510MatchesHas the prefix 5051 plus one suffix digit
5051234MatchesHas the prefix plus several suffix digits
5051Does not matchThe dot requires at least one additional character
60510Does not matchThe 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)
  • 7 must be the first digit.
  • [1-3] consumes exactly one second digit.
  • The second digit can be 1, 2, or 3.

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)
  • X requires 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.
ValueResultReason
10MatchesOne leading digit plus one following character
2345MatchesStarts with a digit and has a suffix
7Does not matchNo character is available for the dot
AB12Does not matchThe first character is not a digit

Pattern Matching Results at a Glance

PatternRequired FormatMatching ExamplesNonmatching ExamplesTypical Use
_NXXXXXXSeven digits; first is 2-92345678, 98765431234567, 234567NANP-style local numbers
_9ZXXXXXXX9, then 1-9, then seven digits912345678, 999999999901234567, 812345678Access prefix followed by a constrained number
_5051.5051 plus at least one character50510, 50512345051, 60510Variable values beginning with a fixed prefix
_7[1-3]7 followed by exactly 1, 2, or 371, 72, 7370, 74, 711Small range of related extensions
_X.One digit plus at least one following character10, 23457, AB12Broad 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

SymptomLikely CauseResolution
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.
  • N means 2-9, Z means 1-9, and X means 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.
  • _NXXXXXX requires 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.