Regex is killing me Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
4evernoob
Posts: 7
Joined: 15 Aug 2022, 15:11

Regex is killing me

Post by 4evernoob » 15 Aug 2022, 16:17

I'm trying to replace

Code: Select all

["port"] = 10002
with

Code: Select all

["port"] = DCSP
but it's dying on the RegExReplace line. Can anyone see what I'm doing wrong? I have tried a lot of different itterations of this but it keeps dying on the regex.

Code: Select all

Random, DCSP, 10100, 10300
FileRead, Text, C:\Config\serverSettings.lua
Text:=RegExReplace(Text, ["port"] = \d*, ["port"] = DCSP,)
FileDelete C:\Config\serverSettings.lua
FileAppend, %Text%, C:\Config\serverSettings.lua
I get error on the RegExReplace line

Line Text: \d
Error: The leftmost character above is illegal in an expression

sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Regex is killing me

Post by sofista » 15 Aug 2022, 16:51

Assuming that the port number may change, try:

Code: Select all

Text = ["port"] = 1002
Text := RegExReplace(Text, "\[""port""] = \K\d+", "DCSP")
MsgBox, % Text    ; ["port"] = DCSP
or expression mode (recommended):

Code: Select all

Text := "[""port""] = 1002"
Text := RegExReplace(Text, "\[""port""] = \K\d+", "DCSP")
MsgBox, % Text    ; ["port"] = DCSP

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Regex is killing me

Post by flyingDman » 15 Aug 2022, 17:04

I guess the OP wanted DCSP to be a random number. In that case:

Code: Select all

Text := "[""port""] = 1002"
Random, DCSP, 10100, 10300
text:=RegExReplace(Text, "\[""port""] = \K\d*", DCSP)
msgbox % text
(but credit to @sofista )
14.3 & 1.3.7

4evernoob
Posts: 7
Joined: 15 Aug 2022, 15:11

Re: Regex is killing me

Post by 4evernoob » 15 Aug 2022, 17:43

No Text is read in from a file that contains this and more.

Code: Select all

        ["maxPing"] = 0,
        ["resume_mode"] = 1,
        ["event_Role"] = true,
    }, -- end of ["advanced"]
    ["port"] = 10102,
    ["listLoop"] = false,
    ["uri"] = "startServer",
    ["isPublic"] = true,
I need to change the "port" line to a different number that is randomly generated and save the file.
This did the trick though. Thanks a bunch!
:superhappy:

sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Regex is killing me

Post by sofista » 15 Aug 2022, 19:57

@4evernoob Glad it worked for you.
@flyingDman Good catch!

4evernoob
Posts: 7
Joined: 15 Aug 2022, 15:11

Re: Regex is killing me

Post by 4evernoob » 13 Sep 2022, 08:04

Still dying here. LOL.
Difference between this one and the last is the number at the end of the line is surrounded by quotes. Thought it would be easy to use last example but, not.
I'm trying to replace the number on this line

Code: Select all

["tacviewRemoteControlPort"] = "43562",
This is as close as I've been able to come

Code: Select all

	text:=RegExReplace(Text, "\[""tacviewRemoteControlPort""] = ""\K\d*""", TACRC)
but I come up short missing the final quote, everything else I have tried fails compile.

Code: Select all

["tacviewRemoteControlPort"] = "43562,
I apologize if I'm breaking a rule by piling on to this thread but seemed appropriate.

sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Regex is killing me  Topic is solved

Post by sofista » 13 Sep 2022, 10:01

Try this:

Code: Select all

text = ["tacviewRemoteControlPort"] = "43562",
TACRC := 12345
text:=RegExReplace(Text, "\[""tacviewRemoteControlPort""] = ""\K\d*(?="")", TACRC)
MsgBox, % text    ; ["tacviewRemoteControlPort"] = "12345",

teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Regex is killing me

Post by teadrinker » 13 Sep 2022, 11:09

Code: Select all

text1 = ["port"] = 10102
text2 = ["tacviewRemoteControlPort"] = "43562",
TACRC := 12345

MsgBox, % RegExReplace(text1, "\["".+?""]\s=\s""?\K\d+", TACRC) . "`n"
        . RegExReplace(text2, "\["".+?""]\s=\s""?\K\d+", TACRC)

Post Reply

Return to “Ask for Help (v1)”