Regex

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AngryPig

Regex

23 Mar 2018, 11:47

I have a file with a bunch of text which is the output from a command prompt. I want to find the last occurrence of a string as a progress measure.

The file at points will say "some number" of "total processes" eg. blah blah 10 of 21 blah blue. I tried using regex but with no luck.

Here is my attempt (it always returns 0)

Code: Select all

FoundPos := RegExMatch(tipUpdate, "[0-9]+ of [0-9]+", ,StartingPosition := 0)
Any help?
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Regex

23 Mar 2018, 12:08

Code: Select all

str := "The file at points will say 'some number' of 'total processes' eg. blah blah 10 of 21 blah blue. I tried using regex but with no luck."
FoundPos := RegExMatch(str, "[0-9]+ of [0-9]+",i)
MsgBox % "Position :`t" FoundPos "`nString:`t>>" i "<<"
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
Hello2215

Re: Regex

23 Mar 2018, 12:15

Odlanir wrote:

Code: Select all

str := "The file at points will say 'some number' of 'total processes' eg. blah blah 10 of 21 blah blue. I tried using regex but with no luck."
FoundPos := RegExMatch(str, "[0-9]+ of [0-9]+",i)
MsgBox % "Position :`t" FoundPos "`nString:`t>>" i "<<"
This only grabs the first instance in my file :(
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Regex

23 Mar 2018, 12:16

a little modification to the solution provided by Odlanir to catch the last occurrence per OP.

Code: Select all

str := "The file at points will say 'some number' of 'total processes' eg. blah blah 10 of 21 blah blue. I tried using regex but with no luck. blah blah 11 of 21 blah blue"
FoundPos := RegExMatch(str, ".*\K\b[0-9]+ of [0-9]+",i)
MsgBox % "Position :`t" FoundPos "`nString:`t>>" i "<<"
AngryPig
Posts: 3
Joined: 23 Mar 2018, 12:15

Re: Regex

23 Mar 2018, 12:20

AlphaBravo wrote:a little modification to the solution provided by Odlanir to catch the last occurrence per OP.

Code: Select all

str := "The file at points will say 'some number' of 'total processes' eg. blah blah 10 of 21 blah blue. I tried using regex but with no luck. blah blah 11 of 21 blah blue"
FoundPos := RegExMatch(str, ".*\K\b[0-9]+ of [0-9]+",i)
MsgBox % "Position :`t" FoundPos "`nString:`t>>" i "<<"

Thanks, that worked :D can you please explain a little what the \K\b does? I do no understand that part
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Regex

23 Mar 2018, 13:13

https://autohotkey.com/docs/misc/RegEx- ... tm#Options

The escape sequence \K is similar to a look-behind assertion because it causes any previously-matched characters to be omitted from the final matched string. For example, foo\Kbar matches "foobar" but reports that it has matched "bar".

\b means "word boundary", which is like an anchor because it doesn't consume any characters. It requires the current character's status as a word character (\w) to be the opposite of the previous character's. It is typically used to avoid accidentally matching a word that appears inside some other word. For example, \bcat\b doesn't match catfish, but it matches cat regardless of what punctuation and whitespace surrounds it. Capital \B is the opposite: it requires that the current character not be at a word boundary.
User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Regex

23 Mar 2018, 13:25

in addition to what swagfag wrote, here is how it works:

[0-9]+ matches 12 in xx 12 yy 34 zz, the first available match.
.*[0-9]+ matches xx 12 yy 34 in xx 12 yy 34 zz
It means match as much as possible from the beginning of the string as long as it ends with [0-9]+

\K resets start of match, basically disregard everything before it from the match results.
.*\K[0-9]+ matches 4 in xx 12 yy 34 zz
It means match as much as possible from the beginning of the string as long as it ends with [0-9]+, ignore everything before \K
note that it only matched 4 not 34 as .*\K matched xx 12 yy 3 (as much as possible then Ignore) and [0-9]+ matched 4 (good enough!)

\b word boundary, a simple explanation would be a zero width character before and after each word.
i.e. represented by "|", xx 12 yy 34 zz is really |xx| |12| |yy| |34| |zz|

so finally:
.*\K\b[0-9]+ matches 34 in xx 12 yy 34 zz
.*\K matches xx 12 yy (including space after yy) then ignores match results.
\b matches zero width character just before 34
[0-9]+ matches 34

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mebelantikjaya and 317 guests