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@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.