Regex help... Match ini key with certain text Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Regex help... Match ini key with certain text

Post by kunkel321 » 19 May 2022, 17:34

Hi Folks,
I'm not sure if a "Regex Learning Disability" is a real thing... But if it is, I have it! LOL

I'm working with sections from INI files that get assigned to a variable, then manipulated. At one stage, I need to locate the word "Optional" in the first key name that it appears in, and replace the word with "OptX".

The rest of the key name should remain. So if the key is "Key1 Optional key=" it gets changed to "Key1 OptX key=". However, if "Optional" appears in a key value, that should be ignored. I can't simply use the regex .*Optional.*=, because that will erroneously match the word in

Code: Select all

key1 blah key=value Optional this value
key2=blah blah
I guess I need the regex to match zero or more chars but not a linefeed, then Optional, then zero or more chars but not a linefeed (??) Also, I need capture groups so that the unchanged parts of the key name can get put back with "OptX."

Does that make sense? My feeble attempt is below.

Code: Select all

; Not working code.
MyEntry = 
(
key1 Optional key=value Optional value
key2=blah
Also Optional blah
)
MyEntry := RegExReplace(MyEntry, "i)(.*(?!\R))Optional(.*(?!\R)=)", "$1OptX$2",,1)
MsgBox % MyEntry
ste(phen|ve) kunkel

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Regex help... Match ini key with certain text

Post by boiler » 19 May 2022, 17:45

Use ? after the * quantifiers to make them "ungreedy."

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

Re: Regex help... Match ini key with certain text

Post by sofista » 19 May 2022, 19:17

Or add the ungreedy option, as iU). Beware that as the other regex options, it is case sensitive.

Descolada
Posts: 1101
Joined: 23 Dec 2021, 02:30

Re: Regex help... Match ini key with certain text  Topic is solved

Post by Descolada » 19 May 2022, 23:19

I guess the easiest way would be to check if the match is followed by an = sign, because usually in INI files values don't contain these.

Code: Select all

MyEntry := RegExReplace(MyEntry, "i)Optional(?=[^\n\r]*=)", "OptX")
But if you want to make sure that your word is definitely inside a key:

Code: Select all

MyEntry := RegExReplace(MyEntry, "im)^[^\n\r=]*\KOptional(?=[^\n\r]*=)", "OptX")

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Regex help... Match ini key with certain text

Post by BoBo » 20 May 2022, 00:00

I'm not sure if a "Regex Learning Disability" is a real thing... But if it is, I have it! LOL
Welcome to the club :crazy: :shh: :shifty:

Code: Select all

MyEntry = 
(
key1 Optional key=value Optional value
key2=blah
Also Optional blah
)
Loop, parse, MyEntry, `r`n
   MyEntry .= StrReplace(StrSplit(A_LoopField,"=").1,"Optional", "OptX") "`n"
MsgBox % MyEntry
RegExFree + not tested :shh:

User avatar
kunkel321
Posts: 976
Joined: 30 Nov 2015, 21:19

Re: Regex help... Match ini key with certain text

Post by kunkel321 » 20 May 2022, 14:11

Thank you everyone! I tried all of these and they all work. Just fyi that Bobo's loop is backwards though... It changes every occurrence that is NOT in a key name.
ste(phen|ve) kunkel

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Regex help... Match ini key with certain text

Post by BoBo » 20 May 2022, 14:22

kunkel321 wrote:
20 May 2022, 14:11
Just fyi that Bobo's loop is backwards though... It changes every occurrence that is NOT in a key name.

Code: Select all

#SingleInstance, Force

MyEntry = 
(
key1 Optional key=value Optional value
key2=blah
Also Optional blah
)
Loop, parse, MyEntry, `r`n
   m .= StrReplace(StrSplit(A_LoopField,"=").1,"Optional", "OptX") "`n"
MsgBox % m
:shock: I shouldn't have used the MyEntry var for input and output :crazy: . This code is delivering the expected results.

Post Reply

Return to “Ask for Help (v1)”