[asterisk-users] escaping regular expression
Klaus Darilion
klaus.mailinglists at pernau.at
Thu Feb 5 01:50:35 CST 2009
D Tucny schrieb:
> 2009/2/4 Klaus Darilion <klaus.mailinglists at pernau.at
> <mailto:klaus.mailinglists at pernau.at>>
>
>
>
> D Tucny schrieb:
> > 2009/2/4 Klaus Darilion <klaus.mailinglists at pernau.at
> <mailto:klaus.mailinglists at pernau.at>
> > <mailto:klaus.mailinglists at pernau.at
> <mailto:klaus.mailinglists at pernau.at>>>
> >
> > Hi!
> >
> > I am going nuts using REGEXP. I just want to verify if a variable
> > contains a valid +E164 phone number.
> >
> > These, the the pattern is ^\+[0-9]+
> >
> > First I tried:
> >
> > Set(pattern=^\+[0-9]+);
> > if (${REGEX("${pattern}" ${${var}})})
> >
> > but that does not work, the backslash is removed, as seen in
> the log
> > file:
> >
> > func_strings.c: FUNCTION REGEX (^+[0-9]+)(+4312345
> > <http://www.adaanumber.com/>)
> >
> > So, meanwhile I tried to escape the backslash. I tried:
> > Set(pattern=^\\+[0-9]+);
> > Set(pattern=^\\\+[0-9]+);
> > Set(pattern=^\\\\+[0-9]+);
> >
> > But always the same result:
> >
> > func_strings.c: FUNCTION REGEX (^+[0-9]+)(+4312345)
> >
> > How can I solve this problem?
> >
> >
> > Try something like... pattern=^[+]\{1\}[0-9]+
>
> Are you sure? The \ should be in front of the +
>
>
> Pretty sure...
>
> exten => *56,1,NoOp("Starting regexp test")
> exten => *56,n,Set(pattern=^[+]\{1\}[0-9]+)
> exten => *56,n,Set(var=123456789)
> exten => *56,n,NoOp(${IF(${REGEX("${pattern}" ${var})}?"Match":"No
> Match")}))
> exten => *56,n,Set(var=+123456789)
> exten => *56,n,NoOp(${IF(${REGEX("${pattern}" ${var})}?"Match":"No
> Match")}))
>
>
> [Feb 4 23:49:21] DEBUG[20518] pbx.c: Launching 'NoOp'
> [Feb 4 23:49:21] VERBOSE[20518] logger.c: -- Executing
> [*56 at phonedefault:1] NoOp("SIP/*01-09bd8ff8", ""Starting regexp test"")
> in new stack
> [Feb 4 23:49:21] DEBUG[20518] pbx.c: Launching 'Set'
> [Feb 4 23:49:21] VERBOSE[20518] logger.c: -- Executing
> [*56 at phonedefault:2] Set("SIP/*01-09bd8ff8", "pattern=^[+]\{1\}[0-9]+")
> in new stack
> [Feb 4 23:49:21] DEBUG[20518] pbx.c: Launching 'Set'
> [Feb 4 23:49:21] VERBOSE[20518] logger.c: -- Executing
> [*56 at phonedefault:3] Set("SIP/*01-09bd8ff8", "var=123456789") in new stack
> [Feb 4 23:49:21] DEBUG[20518] func_strings.c: FUNCTION REGEX
> (^[+]{1}[0-9]+)(123456789)
> [Feb 4 23:49:21] DEBUG[20518] pbx.c: Function result is '0'
> [Feb 4 23:49:21] DEBUG[20518] pbx.c: Function result is '"No Match"'
> [Feb 4 23:49:21] DEBUG[20518] pbx.c: Launching 'NoOp'
> [Feb 4 23:49:21] VERBOSE[20518] logger.c: -- Executing
> [*56 at phonedefault:4] NoOp("SIP/*01-09bd8ff8", ""No Match")") in new stack
> [Feb 4 23:49:21] DEBUG[20518] pbx.c: Launching 'Set'
> [Feb 4 23:49:21] VERBOSE[20518] logger.c: -- Executing
> [*56 at phonedefault:5] Set("SIP/*01-09bd8ff8", "var=+123456789") in new stack
> [Feb 4 23:49:21] DEBUG[20518] func_strings.c: FUNCTION REGEX
> (^[+]{1}[0-9]+)(+123456789)
> [Feb 4 23:49:21] DEBUG[20518] pbx.c: Function result is '1'
> [Feb 4 23:49:21] DEBUG[20518] pbx.c: Function result is 'Match'
> [Feb 4 23:49:21] DEBUG[20518] pbx.c: Launching 'NoOp'
> [Feb 4 23:49:21] VERBOSE[20518] logger.c: -- Executing
> [*56 at phonedefault:6] NoOp("SIP/*01-09bd8ff8", "Match)") in new stack
>
> So, the \ is still stripped (ast_app_separate_args removes \), but, it
> doesn't matter as the + is bracketed so it's not the first character
> after the ^ and so regcomp doesn't fail...
Ah, the trick is to put the + into [], so it need not be escaped.
Thanks
Klaus
More information about the asterisk-users
mailing list