[SR-Users] Understanding regex format for subst_hf

Joel Serrano joel at textplus.com
Mon Oct 4 19:18:00 CEST 2021

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.


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/",

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?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.kamailio.org/pipermail/sr-users/attachments/20211004/c1bb29c3/attachment.htm>

More information about the sr-users mailing list