The next concept we'll cover is called pattern matching. 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.
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 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.
Special Characters Used in Pattern Matching
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 letters and characters to represent sets or ranges of numbers. Here are the most common letters:
The letter X or x represents a single digit from 0 to 9.
The letter Z or z represents any digit from 1 to 9.
The letter N or n matches any digit from 2-9.
Ranges used in Pattern Matching
A numeric range can be used to match against a dialed number. This is also called a Character Set
This pattern matches any digit or letter in the brackets. In this example, the pattern will match: 1,2,3,7,8,9
Wilcards used in Pattern Matching
The following special characters are considered wildcards
- . The '.' character matches one or more characters
- ! The '!' character matches zero or more characters immediately
The exclamation mark wildcard (!), behaves specially and will be further explained below in 'Other Special Characters' below.
Please make sure to read 'Be Careful With Wildcards in Pattern Matches' below.