Match One OR Two Digit Extension Not Working As Expected Without Using “dangerous” _. Pattern (Ast 14)

Home » Asterisk Users » Match One OR Two Digit Extension Not Working As Expected Without Using “dangerous” _. Pattern (Ast 14)
Asterisk Users 5 Comments

Back to basics here. I want to match on one OR two digits.

The following two both work, but ONLY for more than one digit, which is not as expected from the docs (see below).

exten => _X.,1,SayNumber(${EXTEN})
exten => _[0-9].,1,SayNumber(${EXTEN})

This next one will ONLY match 2 digits, as expected, but the first two SHOULD match one or more, right?

exten => _XX,1,SayNumber(${EXTEN})

The following pattern works, but I thought it was “dangerous” and to be discouraged?
exten => _.,1,SayNumber(${EXTEN})

So, again, if someone dials 1 and a one second delay passes, I want it to say 1. If someone dials 1 then another 1 within a second then I want it to be
11, and 111 should be invalid.

(I’ve Set(TIMEOUT(digit)=1) )

Yes, I can do this with multiple lines, but the docs suggest this should be easily do-able in 1 line, and I don’t want to double the amount of dialplan (there’ll be a few of these!).

Here are my references:


The letter X or x represents a single digit from 0 to 9. The period character (.) at the end of a pattern matches one or more remaining characters. You put it at the end of a pattern when you want to match extensions of an indeterminate length.


Page 141 of the Asterisk Definitive Guide 4th Edition:

. (period)
Wildcard match; matches one or more characters, no matter what they are. If you’re not careful, wildcard matches can make your dialplans do things you’re not expecting (like matching built-in extensions such as i or h). You should use the wildcard match in a pattern only after you’ve matched as many other digits as possible. For example, the following pattern match should probably never be used:
_. In fact, Asterisk will warn you if you try to use it. Instead, if you really need a catchall pattern match, use this one to match all strings that start with a digit followed by one or more characters (see ! if you want to be able to match on zero or more characters):
_X. Or this one, to match any alphanumeric string:

————————————————— Do not use a pattern of _. as this will match everything including Asterisk special extensions like i, t, h, etc. Instead use something like _X. or _X which will not match __special__ extensions.. So what do you use instead of _. ? Many examples use this construct, but if you use it you may see a warning message in the log advising you to change _. to _X.


5 thoughts on - Match One OR Two Digit Extension Not Working As Expected Without Using “dangerous” _. Pattern (Ast 14)

  • You can use the “!” character :

    exten => _X!,1,SayNumber(${EXTEN})

    Best regards

    Jean Aunis

    Le 13/10/2016 à 12:54, Jonathan H a écrit :

  • Sorry, I should have said, I already tried “!”.

    It matches immediately and doesn’t wait for a second digit.

  • In article , Jonathan H wrote:

    When matching an extension being dialled, Asterisk is only concerned about priority 1, so that’s the only priority you need to double. You should be able to use ! safely in priority 2 upwards:

    exten => _X,1,NoOp(Matching single digit)
    exten => _X.,1,NoOp(Matching multiple digits)
    exten => _X!,2,SayNumber(${EXTEN})
    exten => _X!,3,Etc..

    Disclaimer: I haven’t tested this.

    Cheers Tony

  • Thanks – I appreciate the idea, but it matches more than 2 digits.

    But, thanks to your info, I cobbled together something that works, and matches only 1 or 2 digits in what I think is the most compact way, by sending a 1 digit extension to the 2 digit matcher, prefixed with a 0. Example which works:

    exten => s,1,Set(TIMEOUT(digit)=1)
    same => n,BackGround(extension)
    same => n,WaitExten(5)
    exten => _X,1,Goto(0${EXTEN},1)
    exten => _XZ,1,SayNumber(${EXTEN})
    same => n,Goto(s,1)
    exten => e,1,Goto(s,1)

    ***HOWEVER!!!!*** Do I need to report a bug?


    A: All the documentation is wrong and “. (period) Wildcard match;
    matches one or more characters” is NOT actually how Asterisk works. or B: The documentation is correct and Asterisk’s “one of more character matching” routine is wrong or C: I’ve completely misunderstood what “matches one of more characters” means 🙂

    What’s the consensus? Thanks!

  • In article , Jonathan H wrote:

    As I understand it, there are two different functionalities overloaded on the operators.

    1. The . in a pattern will match one or more digits, but only when WaitExten has exited due to the digit timeout. Even if the number being collected would match the ., it does not do so until Asterisk decides no more digits will be entered.

    2. The ! in a pattern will match zero or more digits, and will also cause WaitExten to be satisfied as soon as it can match the pattern, without waiting for more digits.

    It’s a pity that the choice between one-or-more and zero-or-more is overloaded with wait-for-dialling-pause and match-immediately, rather than being selectable independently. But it’s been like that ever since
    ! was introduced, probably over 10 years ago.

    Cheers Tony