RegExReplace not working as expected

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
chaoscreater
Posts: 60
Joined: 12 Sep 2019, 21:15

RegExReplace not working as expected

26 Feb 2024, 04:54

Hi all,

I've got a regex issue that I've been trying to fix for hours and I can't figure out what I'm doing wrong. Here's the regex pattern and example data:
https://regex101.com/r/X0Oein/1

In my example script below, LocalsContent contains the exact same data as shown in the link above.

Code: Select all

regexComment_Out_Pattern := "^\h*(""ssfsfsubnet_service_endpoint"")\s*=\s*(?:\[[^\]]*|\{[^}]*|""[^""]+)"

clipboard := regexComment_Out_Pattern
msgbox % regexComment_Out_Pattern

; clipboard := LocalsContent
msgbox % LocalsContent

; Replace with # comment block
LocalsContent := RegExReplace(LocalsContent, regexComment_Out_Pattern, "# Commented out by `Terraform_locals_Locals_Manipulator`" . "`n  # $0`n")

msgbox % LocalsContent
The expectation is that the captured data will be replaced, and yet nothing happens at all. If I replace the regex pattern with something simpler, then the captured data gets replaced just fine.

The regex pattern is 100% correct, as when I dump the regex pattern to clipboard and paste it into regex101, it works perfectly.

Any help would be much appreciated.
just me
Posts: 9574
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: RegExReplace not working as expected

26 Feb 2024, 05:29

If you want to edit multiline text using ^ you need to specify the m option and maybe also `a.
chaoscreater
Posts: 60
Joined: 12 Sep 2019, 21:15

Re: RegExReplace not working as expected

26 Feb 2024, 07:07

I've tried this:

Code: Select all

regexComment_Out_Pattern := "`am)^(\h*(" localName ")\s*=\s*(?:\[[^\]]*|\{[^}]*|""[^""]+))"
but it doesn't work still
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: RegExReplace not working as expected

26 Feb 2024, 08:39

Just a comment here. I have previously noticed that regex101 does not use the same default flags as AHK, so this issue about a difference in behavior is commonly noted here on the forum. A fix is often found by adjusting the AHK statement slightly and possibly also adjusting the flags at the Web site (in checking). If your string works there, would have a look at how the "m" and "s" AHK options are being handled. AHK uses Perl-Compatible Regular Expressions (PCRE).

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 256 guests