Forum: CFEngine Help
Subject: Re: cfengine 3.1.5 pcre question
Author: sauer
Link to topic: https://cfengine.com/forum/read.php?3,21784,24146#msg-24146

Well, if this is going to be broughtback from the dead, someone may as well 
link to the Anchored v/s Unachored regexp documentation: 
http://cfengine.com/manuals/cf3-reference.html#Anchored-vs_002e-unanchored-regular-expressions

Specifically, the comment pattern is unanchored - and the file is treated as a 
single multiline string.  You need the end of line anchor if you want to only 
delete to the end of the line.  The reason it's set up this way is so you can 
delete multi-line comments and similar (ie, you can use the /\*.*\*\// pattern 
(the reference manual regexp is much less accurate, IMHO) to delete /* C 
comments */ which span multiple lines).  The dot matches *any* character, 
including an EOL.  So using [^\n]* means "0 or more non-newline characters" and 
the pattern ends when it gets to the end of a line indicated by a \n or by the 
EOF.  Using .* matches until the end of the file.  If the file starts with a 
comment, therefore, the whole file gets discarded as a big ol' multi-line 
comment and you get no results back.

You can probably do #.*$, or if that doesn't work, use \z instead of the $ (ie, 
use #.*\z to trim comments).  The Perl/PCRE \z escape (lowercase z) matches 
just after the newline (which is probably \R to match any unicode line ending 
sequence) at the end of a line, and \z is independent of the multiline mode 
PCRE is using, so it's arguably "safer" than the $ in general.  The upper-case 
\Z matches before and then again after the newline, so if \z makes things 
behave oddly in a specific case, try \Z.

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

Reply via email to