[asterisk-users] a little regex help needed

Atis Lezdins atis at iq-labs.net
Mon Oct 20 15:46:32 CDT 2008


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 :)

Of course there's also direct callerid matching, so you can match
dialed extension and callerid in same rule, but this looks simpler to
me in this case :)

For more info see
http://www.voip-info.org/wiki/view/Asterisk+config+extensions.conf and
search for "ex-girlfriend" :)

Regards,
Atis

-- 
Atis Lezdins,
VoIP Project Manager / Developer,
atis at iq-labs.net
Skype: atis.lezdins
Cell Phone: +371 28806004
Cell Phone: +1 800 7300689
Work phone: +1 800 7502835



More information about the asterisk-users mailing list