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
How to use options in regexreplace when NeedleRegEx is a variable
-
- Posts: 30
- Joined: 13 Feb 2021, 10:34
Re: How to use options in regexreplace when NeedleRegEx is a variable
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.
"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
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)
"\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)
-
- Posts: 30
- Joined: 13 Feb 2021, 10:34
Re: How to use options in regexreplace when NeedleRegEx is a variable
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. I renamed the extension to txt and attached it here. try a fileread in your code and you will see what my problem is
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. I renamed the extension to txt and attached it here. try a fileread in your code and you will see what my problem is
Re: How to use options in regexreplace when NeedleRegEx is a variable
Looks OK here.
My AHK version: 1.1.34.03
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)