<div dir="ltr">Update:<div><br></div><div>I managed to achieve it using the "regex" module with "pcre_match()", that will take negative lookahead nicely.</div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Oct 4, 2021 at 10:18 AM Joel Serrano <<a href="mailto:joel@textplus.com">joel@textplus.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hey guys, <div><br></div><div>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.</div><div><br></div><div><div><b>subst_hf: </b>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 <b>subst.</b><br></div><div><b><br></b></div><div><b>subst: </b>at the beginning it says it uses perl or sed format, but in the '/re/repl/flags' param it says that sed format.</div></div><div><br></div><div><a href="https://www.kamailio.org/docs/modules/devel/modules/textops.html#textops.f.subst_hf" target="_blank">https://www.kamailio.org/docs/modules/devel/modules/textops.html#textops.f.subst_hf</a><br></div><div><br></div><div><div style="color:rgb(0,0,0);font-family:Helvetica,Arial,sans-serif;font-size:15px"><div><div><h3 style="margin:1em 0px 0.75em;padding:0px 0px 5px 5px;color:rgb(107,83,68);font-family:Scada,Helvetica,sans-serif;font-size:20px;line-height:28px;font-style:italic">4.20.  <code>subst_hf(hf, subexp, flags)</code></h3></div></div></div><p style="font-family:Helvetica,Arial,sans-serif;text-align:justify;font-size:15px"><font color="#ff0000">Perl-like substitutions</font><span style="color:rgb(0,0,0)"> in the body of a header field.</span></p><p style="font-family:Helvetica,Arial,sans-serif;text-align:justify;color:rgb(0,0,0);font-size:15px">Meaning of the parameters is as follows:</p><div style="font-family:Helvetica,Arial,sans-serif;font-size:15px"><ul type="disc"><li style="color:rgb(0,0,0)"><p style="text-align:justify"><span><em>hf</em></span> - header field name.</p></li><li><p style="text-align:justify"><span style="color:rgb(0,0,0)"><em>subexp</em></span><font color="#000000"> - substitution expression </font><font color="#ff0000">in the same format as of the 'subst' function</font><font color="#000000"> parameter.</font></p></li><li style="color:rgb(0,0,0)"><p style="text-align:justify"><span><em>flags</em></span> - 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.</p></li></ul></div></div><div><br></div><div><div style="color:rgb(0,0,0);font-family:Helvetica,Arial,sans-serif;font-size:15px"><div><div><h3 style="margin:1em 0px 0.75em;padding:0px 0px 5px 5px;color:rgb(107,83,68);font-family:Scada,Helvetica,sans-serif;font-size:20px;line-height:28px;font-style:italic">4.16.  <code>subst('/re/repl/flags')</code></h3></div></div></div><p style="font-family:Helvetica,Arial,sans-serif;text-align:justify;font-size:15px"><span style="color:rgb(0,0,0)">Replaces re with repl (</span><font color="#ff0000">sed or perl like</font><font color="#000000">).</font></p><p style="font-family:Helvetica,Arial,sans-serif;text-align:justify;color:rgb(0,0,0);font-size:15px">Meaning of the parameters is as follows:</p><div style="font-family:Helvetica,Arial,sans-serif;font-size:15px"><ul type="disc"><li><p style="text-align:justify"><span style="color:rgb(0,0,0)"><em>'/re/repl/flags'</em></span><font color="#000000"> - </font><font color="#ff0000">sed like regular expression</font><font color="#000000">. flags can be a combination of i (case insensitive), g (global) or s (match newline don't treat it as end of line).</font></p><p style="color:rgb(0,0,0);text-align:justify">'re' - is regular expression</p><p style="color:rgb(0,0,0);text-align:justify">'repl' - is replacement string - may contain pseudo-variables</p><p style="color:rgb(0,0,0);text-align:justify">'flags' - substitution flags (i - ignore case, g - global)</p></li></ul></div></div><div><br></div><div><br></div><div><br></div><div><br></div><div>I'm trying to use this:</div><div><br></div><div>subst_hf("X-myheader", "/^(?!string1 |example2-|text3 )(.*)/myprefix \1/", "a")<br></div><div><br></div><div>In an online tester using PCRE, the format is correct, but Kamailio doesn't like it:</div><div><br></div><div>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/<br></div><div><br></div><div><br></div><div>If I remove the negative lookahead chars (?!) then kamailio doesn't complain but the regex doesn't do what I want.</div><div><br></div><div>Does this mean that Kamailio won't support negative lookahead for subst regex replacements?</div><div><br></div><div>What I'm trying to achieve is something along:</div><div><br></div><div>"If the value of the header Myheader doesn't start with ABC, DEF or HIJ, then prefix it with XYZ"</div><div><br></div><div>Any suggestions/ideas on how to achieve this?</div><div><br></div><div><br></div><div>Thanks, </div><div>Joel.</div><div><br></div><div><br></div><div><br></div></div>
</blockquote></div>