Regex on "Caller ID" on Inbound Routes

Hi everyone,

I want your opinions then your suggestions about this.
I’m using Asterisk 13.4 and “Freepbx as GUI” (It’s an obligation, I really want to work without it because I want to manipulate directly Asterisk).

My object: Block Specific "Caller IDs " using “Inbound Route Module”.
The problem here is That I can’t use “Blacklist” module because I want to block "Specific Caller Ids " on “Specific DIDs”. So, I’m obliged to use “Inbound Route Module” in Freepbx. But, this module is very limited when you want to specify “REGEX” on “Caller ID” field. And from my researches , this limit on “Regex” is in Asterisk also, because for example it allows you to specify if caller id “START with: _89XXXX” or “END with: _XXXX89” but for example can’t allow you to specify “number1 | number2 | number3” (for caller ids which don’t have pattern in between) like the normal regex allows you in “Shell Scripts for example”…

Might be I understood quite good how “Pattern” work in “Asterisk Language” or something else. So, I want some advices here about:

  • If I keep using Freepbx GUI: I must create “Inbound Route” for each “Caller ID” , which is in my opinion very very very bad workaround; because If I had 1000 different DIDs and in each DID I want to block 1000 "Caller ID "===> it ends with 1000000 In Rules !!!

    ** If so, this won’t affect “Asterisk performance” ? and if so, How can I prevent this ? (sorry to ask this Freepbx Related problem here , because I’m certain that Freepbx Stuff can’t understand the limit of Asterisk code or instructions).

    ** If not affecting it’s performance, I want to know what is the difference between putting “All extensions tests” in one context in the dialplan and trying to divide dialplan in “multi contexts” like doing this Freepbx (for example “InRoule1”, “InRoule2”…) on “Asterisk performance” level.

  • If I want to develop an asterisk code doing what I want, I need to optimize my dialplan using “REGEX” function to test if "Caller Id " matches one of “list Numbers” instead of doing a context for each “Caller ID”.
    So can you help me understanding how “REGX” function works in “ASterisk” and how can I use it in the Dialplan through examples , if it’s possible of course.

-And I want to know if “Regx” has side effects on Asterisk performance?

Thanks in advance.

There are a couple ways you could do this.

You could use slash matching on your DID’s

I whitelist calls from in my state to my extension and everything else goes to voicemail.

exten => 7124/_480NXXXXXX,1,Goto(RING)
exten => 7124/_520NXXXXXX,1,Goto(RING)
exten => 7124/_602NXXXXXX,1,Goto(RING)
exten => 7124/_623NXXXXXX,1,Goto(RING)
exten => 7124/_928NXXXXXX,1,Goto(RING)
exten => 7124,1,NoOP()
 same =>      n,Ringing(16)
 same =>      n,Voicemail(7124@kiniston,u)
 same =>      n,Hangup()
 same =>      n(RING),Gosub(stdexten,SIP/7124kiniston)

This would have to be on each extension you wish to do a whitelist/blacklist on however.

Or you could do this externally via FUNC_ODBC and a SQL database where you check on each inbound call if the caller is allowed to the destination

readsql=SELECT COUNT(SOURCE) from BLACKLIST where SOURCE = ‘${SQL_ESC(${ARG1})}’ and DESTINATION = ’ ‘${SQL_ESC(${ARG2})}’

Assuming you have a database setup and the numbers in there.

1 Like

You can use the : operator in expressions to do regular expression check using Unix regular expressions. I haven’t checked whether it supports | (possibly as |).

1 Like

Yes exactly, this is what is exactly generated when using “InbounRoute” on Freepbx. But the problem is I can’t put regex after the slash "[quote=“johnkiniston, post:2, topic:68509”]
exten => 7124/_602NXXXXXX,1,Goto(RING)


==> the only permitted regex here is “Begin with” or “end with” or “range of numbers” , But when you need to specify more than one number ==> so you need “or” operator , you can’t use it.

for example I need: to do this “exten => 7124/REGEX(_928NXXXXXX| clid1| clid2) ,1,Goto(RING)”
==> something like that. But unfortunately, I can’t find any example using that.

As I mentioned before, I can create “InboundRoute” for each CallerID, But I want to summarize all the cases in one Route (for each DID) to prevent only “Asterisk Performance Leak” if it might happen .

That is exactly what I want, “Linux Regex”. But I can’t find an example of using that on controlling “Caller ID”.
As I’m not a Pro on Asterisk language: All my attempts doing that using “REGEX” Asterisk function failed.

Can you guide please more.

IS there any side effects on Asterisk Performance when I have huge number of Routes (IN or Out) registered??

I recommend you ask on the FreePBX community site at

People that follow this forum like me are not as familiar with their GUI and dialplans as the people there.