Forum: CFEngine Help
Subject: Re: problem with negative lookahead regex
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,24188,24194#msg-24194
Hi Sven,
The problem is that the regular expression engine tries its hardest to match
the string. In you case, the .*? gobbles the rest of the line, and the (?!,
SYSLOG), being a *zero-length* negative lookahead, matches an empty string at
the end. One solution is to enclose the whole part after the equals sign in the
negative lookahead, like this:
"matched_origin" expression => regcmp("^log4j\.rootLogger=(?!.*?,
SYSLOG).*$", "log4j.rootLogger=INFO, FILE");
"matched_whole" expression => regcmp("^log4j\.rootLogger=(?!.*?,
SYSLOG).*$", "log4j.rootLogger=INFO, FILE, SYSLOG");
Note how the negative lookahead includes everything after the equals sign. In
this case the ", SYSLOG" part is not optional within the NLA - if it's there it
will be seen by the regex. In this case it is important to add the ".*" at the
end of the regex, so that anything that actually follows the = sign is matched.
In this particular example (not sure if it matches the real use you want to
give to this), you could also negate the result of regcmp() and use a much
simpler, positive regex:
"matched_origin" not => regcmp("^log4j\.rootLogger=.*, SYSLOG$",
"log4j.rootLogger=INFO, FILE");
"matched_whole" not => regcmp("^log4j\.rootLogger=.*, SYSLOG$",
"log4j.rootLogger=INFO, FILE, SYSLOG");
Hope this helps,
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine