[asterisk-users] a little regex help needed

sean darcy seandarcy2 at gmail.com
Mon Oct 20 18:38:55 CDT 2008


Atis Lezdins wrote:
> On Mon, Oct 20, 2008 at 9:20 PM, Jared Smith <jsmith at digium.com> wrote:
>> On Mon, 2008-10-20 at 14:10 -0400, sean darcy wrote:
>>> exten =>s,n,Set(CALLERID(name)=${IF($[0${CALLERID(num)} =
>>> 0${REGEX("21245711*")} ] ? "Office":${CALLERID(name)} )})
>> [snip]
>>
>>> What I'd expect is a callerid(num) of 2124571123 to generate an if test
>>> of  [02124571123 == 021245711*] or TRUE.
> 
> This is not the correct way to use regular expressions. Regular
> expression is matched to data withing REGEX function, and it just
> returns match/don't match.
> 
> Here's description
> 
> REGEX("<regular expression>" <data>)
> 
> [Synopsis]
> Regular Expression
> 
> [Description]
> Returns 1 if data matches regular expression, or 0 otherwise.
> Please note that the space following the double quotes separating the
> regex from the data
> is optional and if present, is skipped. If a space is desired at the
> beginning of the data,
> then put two spaces there; the second will not be skipped.
> 
> So, it would be something like:
> 
> ${REGEX("21245711.*" ${CALLERID(num)})}
> 
>>> But I've messed up the regex statement somehow.
>> In regular expressions, the * means zero or more of the preceding
>> character, so the way you have that written means "021245711 and zero or
>> more 1s".  What you want instead is "021245711.*", which means
>> "021245711 followed by at least on other character".
>>
> correction - 021245711.* would match also "021245711" as * allows zero
> or more and dot means any character.
> 
>> Hopefully that sets you on the right path.  Don't forget that Asterisk
>> has two regex operators that can be used in expressions as well...
>> they're the ':' and '~' operators.
> 
> I wonder what are those used for? Never heard of that.
> 
> Are you really sure you need regular expressions there? Asterisk has
> it's own number pattern matching, as it's much easier to read, and
> would allow easy adding/removing some specific masks. Here's one
> sample:
> 
> [main]
> ..
> exten => s,n,GoSub(callerid-update,${CALLERID(num)},1)
> ..
> 
> 
> [callerid-update]
> exten => 021245711XX,1,Set(CALLERID(name)=Office: ${CALLERID(num)});
> exten => 021245711XX,2,Return();
> exten => _X.,1,Return();
> exten => i,1,Return(); // just for safety :)
> 

Exactly where I was trying to go. I was thinking a little differently 
though:

[any-incoming-context]
exten => s,1,Answer()
exten => s,2,Gosub(set-callerid-name,1,1)
exten => s,3,Dial(......


[set-callerid-name]
exten=>1,1,Set(CALLERID(name)=${IF($[0${CALLERID(num)} = ...
exten=>1,n,Set(CALLERID(name)=${IF($[0${CALLERID(num)} = ...
.........
.........
exten => 1,n,Return()


which seemed easier ( and easier to read) since I didn't have to insert 
Return()'s every other line.

sean




More information about the asterisk-users mailing list