[SR-Users] Understanding regex format for subst_hf

Bastian Triller bastian.triller at gmail.com
Wed Oct 6 08:44:30 CEST 2021


since your using an "if (...)" anyways, you can prolly do this w/ if
(!($hdr(X-My) =~ "^(one|two|three)")) subst_hf("X-My", "/^(.)/prefix \1/",
"a");

Regards,
Bastian

On Mon, Oct 4, 2021 at 7:47 PM Joel Serrano <joel at textplus.com> wrote:

> Update:
>
> I managed to achieve it using the "regex" module with "pcre_match()", that
> will take negative lookahead nicely.
>
>
>
> On Mon, Oct 4, 2021 at 10:18 AM Joel Serrano <joel at textplus.com> wrote:
>
>> Hey guys,
>>
>> I'm trying to do a replacement regex using subst_hf but I can't seem to
>> get the format right and the docs are confusing.
>>
>> *subst_hf: *docs say it uses perl-like regex (as I understand it, that
>> would mean PCRE). Docs also say in the 'subexp' param that it uses the same
>> format as *subst.*
>>
>> *subst: *at the beginning it says it uses perl or sed format, but in the
>> '/re/repl/flags' param it says that sed format.
>>
>>
>> https://www.kamailio.org/docs/modules/devel/modules/textops.html#textops.f.subst_hf
>>
>> 4.20.  subst_hf(hf, subexp, flags)
>>
>> Perl-like substitutions in the body of a header field.
>>
>> Meaning of the parameters is as follows:
>>
>>    -
>>
>>    *hf* - header field name.
>>    -
>>
>>    *subexp* - substitution expression in the same format as of the
>>    'subst' function parameter.
>>    -
>>
>>    *flags* - control flags - it has to be one of: a - all headers
>>    matching the name; f - only first header matching the name; l - only the
>>    last header matching the name.
>>
>>
>> 4.16.  subst('/re/repl/flags')
>>
>> Replaces re with repl (sed or perl like).
>>
>> Meaning of the parameters is as follows:
>>
>>    -
>>
>>    *'/re/repl/flags'* - sed like regular expression. flags can be a
>>    combination of i (case insensitive), g (global) or s (match newline don't
>>    treat it as end of line).
>>
>>    're' - is regular expression
>>
>>    'repl' - is replacement string - may contain pseudo-variables
>>
>>    'flags' - substitution flags (i - ignore case, g - global)
>>
>>
>>
>>
>>
>> I'm trying to use this:
>>
>> subst_hf("X-myheader", "/^(?!string1 |example2-|text3 )(.*)/myprefix
>> \1/", "a")
>>
>> In an online tester using PCRE, the format is correct, but Kamailio
>> doesn't like it:
>>
>> Oct  4 09:29:16 cops-n1 cops[15957]: ERROR: <core> [core/re.c:285]:
>> subst_parser(): bad regular expression ^(?!string1 |example2-|text3 )(.*)
>> in /^(?!string1 |example2-|text3 )(.*)/myprefix \1/
>>
>>
>> If I remove the negative lookahead chars (?!) then kamailio doesn't
>> complain but the regex doesn't do what I want.
>>
>> Does this mean that Kamailio won't support negative lookahead for subst
>> regex replacements?
>>
>> What I'm trying to achieve is something along:
>>
>> "If the value of the header Myheader doesn't start with ABC, DEF or HIJ,
>> then prefix it with XYZ"
>>
>> Any suggestions/ideas on how to achieve this?
>>
>>
>> Thanks,
>> Joel.
>>
>>
>>
>> __________________________________________________________
> Kamailio - Users Mailing List - Non Commercial Discussions
>   * sr-users at lists.kamailio.org
> Important: keep the mailing list in the recipients, do not reply only to
> the sender!
> Edit mailing list options or unsubscribe:
>   * https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.kamailio.org/pipermail/sr-users/attachments/20211006/d375e29b/attachment.htm>


More information about the sr-users mailing list