Telecom

Asterisk Dialplan: Regular Expressions (Latest Versions)

MYLINEHUB Team • 2026-02-10 • 9 min

Updated guide for modern Asterisk (PJSIP era): regular expressions with real configs, common mistakes, and troubleshooting steps.

Asterisk Dialplan: Regular Expressions (Latest Versions)

In Asterisk dialplan, regular expressions and pattern matching are used to decide how calls should be routed based on the number dialed, caller ID, time rules, or dynamic conditions.

Without pattern logic, you would need to write hundreds or thousands of individual extensions, which is impossible in real call center deployments.

This guide explains how modern Asterisk pattern matching works, how it differs from classic programming regex, and how to use it safely in PJSIP-based production systems.

Dialplan Pattern Matching vs Traditional Regex

Asterisk does not use full PCRE-style regular expressions inside extension definitions.

Instead, it provides a simplified telephony pattern syntax designed specifically for phone numbers.

Full regex can still be used in functions like:

  • REGEX()
  • FILTER()
  • GotoIf() conditions

Basic Dialplan Pattern Syntax

Patterns always begin with an underscore:

_pattern

Common symbols:

  • X → any digit (0–9)
  • Z → digits 1–9
  • N → digits 2–9
  • . → one or more digits
  • ! → wildcard, immediate match
  • [123] → specific digit list

Common Real-World Number Patterns

4-Digit Internal Extensions

exten => _1XXX,1,Dial(PJSIP/${EXTEN},20)

Indian Mobile Numbers (10 digits starting 6-9)

exten => _[6-9]XXXXXXXXX,1,Dial(PJSIP/${EXTEN}@trunk,60)

Prefix 9 for External Calls

exten => _9.,1,Set(NUM=${EXTEN:1})
 same => n,Dial(PJSIP/${NUM}@trunk,60)

Pattern Matching Order (Critical for Correct Routing)

Asterisk evaluates patterns in this order:

  1. Exact extension match
  2. Best pattern match
  3. First match in dialplan

Incorrect ordering can silently route calls to the wrong destination.

Using REGEX() Function in Dialplan Conditions

Full regular expressions are available through REGEX().

same => n,GotoIf($[${REGEX("^91[0-9]{10}$" ${EXTEN})}]?india)

This checks if the dialed number is:

  • Starts with 91
  • Followed by exactly 10 digits

Filtering Caller ID Using Regex

same => n,GotoIf($[${REGEX("^9" ${CALLERID(num)})}]?vip)

Example usage:

  • VIP routing
  • Spam blocking
  • Country-based filtering

Blocking Invalid Numbers with Pattern Logic

exten => _00.,1,Playback(not-authorized)
 same => n,Hangup()

Prevents international dialing starting with 00.

Using Wildcard “!” Carefully

Example:

exten => _X!,1,NoOp(Catch-all rule)

Warning:

  • Matches immediately
  • Can override correct routing
  • Should be placed last in dialplan

Practical Outbound Routing Example Using Patterns

[outbound]
; Internal extensions
exten => _1XXX,1,Dial(PJSIP/${EXTEN},20)

; Mobile numbers
exten => _[6-9]XXXXXXXXX,1,Dial(PJSIP/${EXTEN}@trunk,60)

; International blocked
exten => _00.,1,Playback(not-authorized)
 same => n,Hangup()

Debugging Pattern Matching Problems

Useful CLI commands:

dialplan show
dialplan show outbound
core set verbose 5

Always confirm:

  • Pattern is correct
  • Context is correct
  • Order of rules is correct

Security Benefits of Proper Pattern Design

  • Prevents toll fraud
  • Restricts international dialing
  • Controls user permissions
  • Ensures compliance with telecom laws

Key Takeaway

Pattern matching is one of the most powerful tools in Asterisk. It allows a small dialplan to control thousands of routing possibilities.

Mastering: dialplan patterns, REGEX(), and rule ordering is essential for building secure, scalable, production-ready telecom systems.

Try it

Want to see API-driven CRM + Telecom workflows in action? Try the WhatsApp bot or explore the demos.

💬 Try WhatsApp Bot ▶️ Watch CRM YouTube Demos
Tip: Comment “Try the bot” on our YouTube videos to see automation in action.