RegExMatch with multiple subpatterns

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
T-Rock
Posts: 27
Joined: 01 Feb 2015, 09:11

RegExMatch with multiple subpatterns

15 May 2021, 10:47

Hi there,

I try to come along with RegExMatch, but I don't get it ...

Code: Select all

; here is the string:
str := "W12345.12 blablabla bla M00000.00 bla"

; the RegExMatch captures only the first subpattern:
FileAppend, % RegExMatch(str, "([WM][0-9]{5}\.[0-9]{2})", Match) ; shows 1 because RegExMatch found something
FileAppend, % Match1 "`n", * ; shows W12345.12
FileAppend, % Match2 "`n", * ; is empty but should be M00000.00
I can't figure out, what's missing ...
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: RegExMatch with multiple subpatterns

15 May 2021, 10:59

What's missing: another pattern enclosed in parentheses.

Code: Select all

str := "W12345.12 blablabla bla M00000.00 bla", RegExMatch(str, "([WM].+?) .*([WM].+?) ", match)
Send %match1% %match2%
T-Rock
Posts: 27
Joined: 01 Feb 2015, 09:11

Re: RegExMatch with multiple subpatterns

15 May 2021, 11:28

Hi mikeyww,

thanks for the quick response and your solution.
I have many of those strings, in which the number of these substrings (like W12345.67) can vary.
So instaed of having the same number of subpatterns, is there a way to define one general pattern which matches all the substrings?
That was my first intention to do so ...
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: RegExMatch with multiple subpatterns

15 May 2021, 11:43

Code: Select all

str := "W12345.12 blablabla bla M00000.00 bla"

MsgBox, % res := RTrim( RegExReplace(str, "s).*?([WM]\d{5}\.\d{2}).*?(?=(?1)|$)", "$1`n"), "`n" )

arr := StrSplit(res, "`n")
for k, v in arr
   MsgBox, % v
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: RegExMatch with multiple subpatterns

15 May 2021, 11:44

Here is one way-- but not as nice as teadrinker's (or maybe I stole this from teadrinker earlier...)!

Code: Select all

str := "W12345.12 blablabla bla M00000.00 bla"
While pos := RegExMatch(str, "[WM].+?(?= )", match, pos ? pos + 1 : 1)
 Send %match%`n
T-Rock
Posts: 27
Joined: 01 Feb 2015, 09:11

Re: RegExMatch with multiple subpatterns

15 May 2021, 11:57

Thank you very much for your help mikeyww and teadrinker.
I was just thinking I should loop through the string by splitting it with the space as delimiter and use always one pattern for these substrings.

That gives me some homework to understand what your solutions are actually doing ... ;)
User avatar
mikeyww
Posts: 26883
Joined: 09 Sep 2014, 18:38

Re: RegExMatch with multiple subpatterns

15 May 2021, 12:50

That would work, too; you could use Loop. You are seeing something comparable in the examples here, though teadrinker's looks at the sequences of digits and dots.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada and 274 guests