regexreplace blanking out my variable

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

regexreplace blanking out my variable

Post by mstrauss2021 » 11 Aug 2022, 19:18

I have used regexreplace in this same program for over a year with much success.
I recently discovered that my main variable "data" was being blanked out. All the info had disappeared.
I traced the problem to one line. Doing a msgbox % data before the line shows the contents of data and doing a msgbox % data after the line shows nothing.
I've spent 4 hours trying to figure out what is wrong and finally decideed I need assistence.
PArt of data that matters:

data :=
<employee>
<name>Daigo Hirashi</name>
<role>Accountant</role>
<thumb>C:\Users\br-rm-6\Desktop\employees\Daigo.jpg</thumb>
</employee>
<resume>
<position>0.000000</position>
<total>0.000000</total>
</resume>

FoundPos1 := RegExMatch(Data, "<actor>" ,, 1)
originalactorpicture1a = <thumb>C:\Users\br-rm-6\Desktop\employees\Daigo.jpg</thumb>
actorpicture1 = <thumb>C:\Users\br-rm-6\Desktop\employees\Daigo_New.jpg</thumb>


CODE:
testactor1pic = <thumb>%originalactorpicture1a%</thumb>
testactor1pic2 = <thumb>%actorpicture1%</thumb

Data := RegExReplace(Data, testactor1pic , testactor1pic2,1,FoundPos1)

msgbox % data = " " at this point

END CODE

Any idea what is going on here? I haven't a clue

btw: I need starting position so I cannot use StrReplace

Thanks

Mike

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

Re: regexreplace blanking out my variable

Post by mikeyww » 11 Aug 2022, 19:30

1. Exactly what actually happens when you run this script?
2. What should happen instead?

See syntax in documentation:

NewStr := RegExReplace(Haystack, NeedleRegEx, Replacement, OutputVarCount, Limit, StartingPos)

Although you can omit some parameters, you cannot omit the commas that mark their place.

Backslash is a special regex character. You can learn about regular expressions in the documentation.

Your starting position is zero?

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

Re: regexreplace blanking out my variable

Post by mstrauss2021 » 11 Aug 2022, 20:34

sorry, mixed up the code with another project.

here is the code. If you copy/paste in your editor you will see what I see. msgbox shows nothing
----------------------------------------------------------------------------------------------------------------------------

Code: Select all

originalactorpicture1a = "<thumb>C:\Users\br-rm-6\Desktop\employees\Daigo.jpg</thumb>"
actorpicture1 = "c:\users\mike\2.jpg"
data := "
<actor>
<name>Daigo Hirashi</name>
<role>Accountant</role>
<thumb>C:\Users\br-rm-6\Desktop\employees\Daigo.jpg</thumb>
</actor>
<resume>
<position>0.000000</position>
<total>0.000000</total>
</resume>"

MsgBox % data

FoundPos1 := RegExMatch(Data, "<actor>" ,, 1)


	testactor1pic = <thumb>%originalactorpicture1a%</thumb>
	testactor1pic2 = <thumb>%actorpicture1%</thumb

	Data := RegExReplace(Data, testactor1pic , testactor1pic2,1,FoundPos1)

	MsgBox % data
[Mod edit: [code][/code] tags added.]

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

Re: regexreplace blanking out my variable

Post by mstrauss2021 » 11 Aug 2022, 20:49

ywa in this example, the starting position is 0

I always use regexreplace on directory paths and never had this problem before

What should happen is <thumb>C:\Users\br-rm-6\Desktop\employees\Daigo.jpg</thumb> in the data variable should change to this c:\users\mike\2.jpg

but instead the data variable is made empty

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

Re: regexreplace blanking out my variable

Post by mikeyww » 11 Aug 2022, 20:58

Suggestions:

1. Read the replies to your post carefully.
2. Display some values of variables in your script.
3. See documentation when helpful.

Code: Select all

originalactorpicture1a := "C:\Users\br-rm-6\Desktop\employees\Daigo.jpg"
actorpicture1          := "c:\users\mike\2.jpg"
data := "
(
<actor>
<name>Daigo Hirashi</name>
<role>Accountant</role>
<thumb>C:\Users\br-rm-6\Desktop\employees\Daigo.jpg</thumb>
</actor>
<resume>
<position>0.000000</position>
<total>0.000000</total>
</resume>
)"
pos := Instr(data, "<actor>")
MsgBox, 64, Starting position, %pos%
testactor1pic  = <thumb>%originalactorpicture1a%</thumb>
testactor1pic2 = <thumb>%actorpicture1%</thumb>
data := RegExReplace(data, "\Q" testactor1pic "\E", testactor1pic2,, 1, pos)
MsgBox, 64, Result, %data%
Explained: Var := expressionRegular expressions

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

Re: regexreplace blanking out my variable

Post by mstrauss2021 » 12 Aug 2022, 09:47

Wow,

I love life. everyday you learn something new. I was looking at the regex options this morning before I checked status of my question. I looked up \Q...\E. I've read this page many times but somehow always missed the part (or maybe just didn't comprehend) what was meant by "Escaping can be avoided"

Thanks so much mikeyww

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

Re: regexreplace blanking out my variable

Post by mikeyww » 12 Aug 2022, 11:51

You are welcome. Other bugs fixed:

1. Missing ":=" in first two lines
2. Lack of line continuation syntax
3. Extraneous <thumb> tags
4. Missing ">" in closing tag
5. Missing parameter in RegExReplace

Post Reply

Return to “Ask for Help (v1)”