Forum: Cfengine Help
Subject: Re: Cfengine 3.1.4 and replace_patterns
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,20414,20420#msg-20420

Nakarin's response is right, and the reason is the same I mentioned the other 
day in this thread, namely:

(?! ...) is a zero-width negative look-ahead assertion. Since it's zero-width, 
it ensures that the text inside the assertion is not present at that point, but 
it does not actually match that text (it does not consume any characters in the 
string). In this case, your original regex:

"^D\{MTAHost\}\[(?!$(g.mailrelay))\].*$"

Would ensure that the value of $(g.mailrelay) does not appear after the bracket 
(as you intended), but it does not actually consume the "not-matched" text, so 
the closing bracket would be expected to come immediately after the opening 
bracket. In effect, this regex would only match the string "D{MTAHost}[]".

By adding the ".*" immediately after the negative look-ahead assertion, 
Nakarin's version allows for any text to follow the opening bracket, thus 
consuming the "text that does not match $(g.mailrelay)" (I know, it can get 
confusing). To make it even more precise, you may want to add the closing 
bracket after the ".*", like this:

"^D\{MTAHost\}\[(?!$(g.mailrelay)).*\]"

(this would be relevant only if there is some text that may come in the line 
after the closing bracket, and that you would like to preserve. If this is the 
case, you would be even safer using the non-greedy version of the regex ".*?")

More details:
http://www.regular-expressions.info/lookaround.html
http://perldoc.perl.org/perlre.html#Look-Around-Assertions

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to