Regex question Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Ph03nix89
Posts: 8
Joined: 19 Jan 2019, 20:34

Regex question

20 Jan 2019, 21:33

Code: Select all

PORT1 = 2
PORT2 = 2
HIDMOUSE_API = 0
Snap mouse = 1
Simple text file. I need to be able to change the values for PORT1 and PORT2 seperately and save the changes to the same file. I know I need to use substr and regex to do this but I can't wrap my head around how. Any help would be appreciated.
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: Regex question

20 Jan 2019, 22:25

everybody, including me, has hard time at the very first time.
there should be lots of ways to achieve your goal.
I'd rather recommend you to take a look at this.

"ini file"
https://www.autohotkey.com/docs/commands/IniRead.htm

Good Luck
Ph03nix89
Posts: 8
Joined: 19 Jan 2019, 20:34

Re: Regex question

21 Jan 2019, 10:14

Hi,

Thanks for the reply.

I should have stated that I am already very familiar with the iniread/iniwrite functions in my first post and how that method wouldn't work in this situation.

As far as I know, they won't work in this situation because the ini in question doesn't follow the standard format in that it doesn't have sections, which is a required field in the iniread and write functions. I am also unable to add sections to the ini as it is part a program that reads the ini as is without sections and I can't change that.

Which is why I'm looking at substr and regex to edit it as a text file instead.

Can anybody help me with the regex and substr method for this?
Or if I'm wrong about the iniwrite method, how do I apply it when there are no section headings present?
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Regex question  Topic is solved

21 Jan 2019, 10:30

Hi,

try:

Code: Select all

text =
(
PORT1 = 2
PORT2 = 2
HIDMOUSE_API = 0
Snap mouse = 1
)
newPORT1 := 3
newPORT2 := 4

Loop 2 {
   text := RegExReplace(text, "m`a)^PORT" . A_Index . "\s*=\s*\K.*$", newPORT%A_Index%)
}
MsgBox, % text
Ph03nix89
Posts: 8
Joined: 19 Jan 2019, 20:34

Re: Regex question

24 Jan 2019, 12:48

teadrinker wrote:
21 Jan 2019, 10:30
Hi,

try:

Code: Select all

text =
(
PORT1 = 2
PORT2 = 2
HIDMOUSE_API = 0
Snap mouse = 1
)
newPORT1 := 3
newPORT2 := 4

Loop 2 {
   text := RegExReplace(text, "m`a)^PORT" . A_Index . "\s*=\s*\K.*$", newPORT%A_Index%)
}
MsgBox, % text
That worked perfectly!
I'm going to read up on the regex funtions provided so I can understand how this works now.
Thanks :)
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Regex question

24 Jan 2019, 13:51

Just for fun and to show an alternative...

Code: Select all

text =
(
PORT1 = 2
PORT2 = 2
HIDMOUSE_API = 0
Snap mouse = 1
)
newPORT1 := 3
newPORT2 := 4

Loop, parse, text, `n
{
	text := StrReplace(text, "PORT1 = 2", "PORT1 ="A_Space . newPort1)
	text := StrReplace(text, "PORT2 = 2", "PORT2 ="A_Space . newPort2)
	break
}

MsgBox, % text
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Regex question

24 Jan 2019, 14:21

SOTE wrote:
24 Jan 2019, 13:51
Just for fun and to show an alternative...
I think that the source text does not necessarily have an exact match "PORT1 = 2", could be "PORT1 = [any number]".
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Regex question

24 Jan 2019, 15:36

teadrinker wrote:
24 Jan 2019, 14:21
SOTE wrote:
24 Jan 2019, 13:51
Just for fun and to show an alternative...
I think that the source text does not necessarily have an exact match "PORT1 = 2", could be "PORT1 = [any number]".
That answer would be below... But just playing around with alternatives...

Code: Select all

text =
(
PORT1 = 2
PORT2 = 2
HIDMOUSE_API = 0
Snap mouse = 1
)
newPORT1 := 3
newPORT2 := 4

Loop, parse, text, `n
{
	if (A_Index = 1)
	{
	Output .= "PORT1 =" A_Space . newPort1 "`n"
	Output .= "PORT2 =" A_Space . newPort2 "`n"
	}
	if (A_Index > 2)
	Output .= A_Loopfield "`n"
}
text :=""
text := Output
MsgBox, % text
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Regex question

24 Jan 2019, 15:47

What if

Code: Select all

PORT1 = 2
PORT2 = 2
are not first and second strings? :)
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Regex question

24 Jan 2019, 23:02

teadrinker wrote:
24 Jan 2019, 15:47
What if

Code: Select all

PORT1 = 2
PORT2 = 2
are not first and second strings? :)
Not exactly sure on that one. But the programmer is likely to know something about the data in which he is interfacing with. Thus know the rows that need to be replaced or some particular attribute that he can pick on.

Was once frustrated by StringReplace not having a wildcard feature :headwall: , so had to use the Voodoo that is regular expressions (RegEx) :crazy: . However, out of convenience, I learned it's not always necessary. Sometimes you can use something that is logically simpler, easier for your non-programming friends to visually understand, or is more apparent to figure out, without consulting the PCRE manual or deciphering that regular expression magic. :trollface:

With that typed, I do use regular expressions, because sometimes there is no other way and regular expression "magic" is the only answer. :dance:
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Regex question

25 Jan 2019, 00:39

Much depends on what magic is to you, something interesting, or something frightening. :o :twisted:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, exodus_cl, ratyrat, Sniperman and 371 guests