Look-ahead regex, not matching?

25 views
Skip to first unread message

Bob

unread,
Jul 8, 2025, 6:50:31 PMJul 8
to help-cfengine
I am trying to change the default screensaver. The line of interest is as follows:

*mode:                  random

Note: there are 3 tab characters between the colon and the word "random".

Code:

body file control
{
    inputs => { "/var/cfengine/share/CoreBase/masterfiles/lib/files.cf" };
}

body common control
{
      bundlesequence => { "example" };
}

bundle agent example
{
  files:
    linux::
      "/etc/X11/app-defaults/XScreenSaver-nogl"
      comment       => "Set screensaver mode to blank",
      edit_line     => my_replace("\*mode:\s+(?!blank)",
                                  "*mode:                       blank"),
      edit_defaults => std_defs;

}
bundle edit_line my_replace(regex,text)
{
replace_patterns:

 "^($(regex))$"

     replace_with => value("$(text)"),
          comment => "Search and replace string";
}


As best as I can tell, I am not matching the line. Any suggestions?

Using CFEngine community 3.24.1.

Thank you.

Nick Anderson

unread,
Jul 8, 2025, 8:54:48 PMJul 8
to help-cfengine
Hi Bob,

Try       edit_line     => my_replace("\*mode:\s+(?!blank$)"

Bob

unread,
Jul 9, 2025, 9:00:25 AMJul 9
to help-cfengine
No help. I also tried to remove the $ from edit_line bundle and that did not appear to matter, either. :(

Bob

unread,
Jul 9, 2025, 10:00:59 AMJul 9
to help-cfengine
When I change it to this, it works:

        edit_line     => my_replace("\*mode:\s+(?!blank.*).*",
                                  "*mode:                       blank"),


In my short test file, it works just fine. However, when I add it to my site.cf file:

   error: Promised replacement '*mode:                  blank' for pattern '^(\*
mode:\s+(?!blank.*).*)$' is not properly convergent while editing '/etc/X11/app-
defaults/XScreenSaver-nogl' (pattern still matches the end-state replacement string '*mode:                     blank', consider use of a negative look ahead)
   error: Errors encountered when actuating replace_patterns promise '^(\*mode:\
s+(?!blank.*).*)$'


What could be the difference?

On Tuesday, July 8, 2025 at 5:54:48 PM UTC-7 ni...@cmdln.org wrote:

Nick Anderson

unread,
Jul 9, 2025, 10:13:51 AMJul 9
to help-cfengine, buha...@gmail.com

Hey Bob,

When I change it to this, it works:

edit_line => my_replace("\*mode:\s+(?!blank.*).*", "*mode: blank"),

In my short test file, it works just fine. However, when I add it to my site.cf file:

error: Promised replacement 'mode: blank' for pattern '^(\ mode:\s+(?!blank.*).*)\(' is not properly convergent while editing '/etc/X11/app- defaults/XScreenSaver-nogl' (pattern still matches the end-state replacement string '*mode: blank', consider use of a negative look ahead) error: Errors encountered when actuating replace_patterns promise '^(\*mode:\ s+(?!blank.*).*)\)'

What could be the difference?

I think it should be like this:

edit_line     => my_replace("\*mode:\s+(?!blank$).*$",

But, I think that this might be of use to you as well: https://gist.github.com/nickanderson/1ffd7936b66b4a7c1e703fbbdc49e0d1

Bob

unread,
Jul 9, 2025, 12:41:41 PMJul 9
to help-cfengine
Thanks, Nick. FYI - I updated to "core" 3.24.2.

I tried the following scenarios after the update:

      edit_line     => my_replace("\*mode:\s+(?!blan.*$).*", 

      edit_line     => my_replace("\*mode:\s+(?!blank.*).*", 
      edit_line     => my_replace("\*mode:\s+(?!blan.+).*",  
      edit_line     => my_replace("\*mode:\s+(?!blank$).*", 

      edit_line     => my_replace("\*mode:\s+(?!blank$).*$",
      edit_line     => my_replace("\*mode:\s+(?!blank).*",

All of which gave the same error (but did perform the edit). On subsequent runs of cf-agent, the file is no longer edited/updated. I suspect this is a glitch of some sort. 

Nick Anderson

unread,
Jul 9, 2025, 12:47:34 PMJul 9
to buha...@gmail.com, help-cfengine

Thanks, Nick. FYI - I updated to "core" 3.24.2.

I tried the following scenarios after the update:

edit_line => my_replace("\*mode:\s+(?!blan.*\().*", edit_line => my_replace("\*mode:\s+(?!blank.*).*", edit_line => my_replace("\*mode:\s+(?!blan.+).*", edit_line => my_replace("\*mode:\s+(?!blank\)).*", edit_line => my_replace("\*mode:\s+(?!blank\().*\)", edit_line => my_replace("\*mode:\s+(?!blank).*",

All of which gave the same error (but did perform the edit). On subsequent runs of cf-agent, the file is no longer edited/updated. I suspect this is a glitch of some sort.

Yeah, I am not good enough with regex to know without running it for myself. But that gist I linked to worked for me when I was playing around last night. Hopefully you can adapt it for yourself.

Reply all
Reply to author
Forward
0 new messages