How to use options in regexreplace when NeedleRegEx is a variable

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mstrauss2021
Posts: 30
Joined: 13 Feb 2021, 10:34

How to use options in regexreplace when NeedleRegEx is a variable

Post by mstrauss2021 » 15 Aug 2022, 10:11

My haystack is definitely multiline.
my needle differs all the time so i use a variable.
I can find the needle with regexmatch by using the m) option but I cant find an example in my research on how to use the m) option with regexreplace when my needle is a variable.
Data := a lot of lines and characters leading to C:\Users\user\testing\Daigo.jpg
test := "C:\Users\user\testing\Daigo.jpg"
test2 := "c:\Snoopy\Jupiter\Scamp.jpg"
MsgBox % RegExMatch(Data, "m)test") Finds the needle

MsgBox % RegExReplace(Data, "\Q" "m)test" "\E", test2,,1306) does not find the needle because it does not get replaced with test2. Even when starting pos = 1 it doesn't find it. The actual position of the needle is 1307

What is the proper syntax to replace something in a multiline haystack when needle is a variable?

Thanks

Mike

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to use options in regexreplace when NeedleRegEx is a variable

Post by mikeyww » 15 Aug 2022, 10:32

Read about expressions. If you don't need a regular expression, or if you are not sure how to use a regular expression, then you can avoid it here, because you are searching for a literal text string.

Code: Select all

Data =
(
abc
def ghi
wer C:\Users\user\testing\Daigo.jpg
jkl mno pqr
l c:\Snoopy\Jupiter\Scamp.jpgadsff
st
)
test  := "C:\Users\user\testing\Daigo.jpg"
test2 := "c:\Snoopy\Jupiter\Scamp.jpg"
MsgBox % Instr(Data, test)
MsgBox % Instr(Data, test2)
"m)test" would look for the literal string test.
"\Q" means that everything following it is literal.
StrReplace

Code: Select all

Data =
(
abc
def ghi
wer C:\Users\user\testing\Daigo.jpg
jkl mno pqr
st
)
test  := "C:\Users\user\testing\Daigo.jpg"
test2 := "c:\Snoopy\Jupiter\Scamp.jpg"
MsgBox, % RegExReplace(Data, "\Q" test "\E", test2,,, 5)

mstrauss2021
Posts: 30
Joined: 13 Feb 2021, 10:34

Re: How to use options in regexreplace when NeedleRegEx is a variable

Post by mstrauss2021 » 15 Aug 2022, 15:37

Interesting.

I copied your code and pasted to my editor, ran and it worked.

I removed your Data variable and read in an nfo file for the Data variable and test2 did not get replaced.

Unfortunately this site wont let me upload an nfo file.
Daigo.txt
this is an nfo file i get data from but had to change the extention
(1.52 KiB) Downloaded 18 times
I renamed the extension to txt and attached it here. try a fileread in your code and you will see what my problem is

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How to use options in regexreplace when NeedleRegEx is a variable

Post by mikeyww » 15 Aug 2022, 20:13

Looks OK here.

Code: Select all

file = %A_ScriptDir%\Daigo.txt
If !FileExist(file) {
 MsgBox, 48, Error, File not found.`n`n%file%
 Return
} Else FileRead, text, %file%
test  := "C:\Users\user\testing\Daigo.jpg"
test2 := "c:\Snoopy\Jupiter\Scamp.jpg"
MsgBox, % Clipboard := RegExReplace(text, "\Q" test "\E", test2,,, 5)
My AHK version: 1.1.34.03

Post Reply

Return to “Ask for Help (v1)”