RegExMatch - AutoHotkey_2.0-a077-c2bb552

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

RegExMatch - AutoHotkey_2.0-a077-c2bb552

07 May 2017, 02:33

Example

Code: Select all

str:= "8 9 7"
pos:= RegExMatch(str, "(\d)", res) ;regex= a group of decimal digits
msgBOX(pos "|" res.count() "|" res.1)
Match anywhere: By default, a regular expression matches a substring anywhere inside the string to be searched.

But RegExMatch found only first (in this case leftmost) digit. 9 and 7 not match.
So result is:
pos == 1
res.count() == 1
res.1 == 8

I think result must be:
pos == 1
res.count() == 3
res.1 == 8
res.2 == 9
res.3 == 7

Question: Is there implementation mistake or _3D_ mistake?
AHKv2.0 alpha forever.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: RegExMatch - AutoHotkey_2.0-a077-c2bb552

07 May 2017, 09:16

_3D_ mistake. You don't understand how regular expressions work. See here:
http://www.regular-expressions.info/

For your specific code, try StrSplit:
https://autohotkey.com/docs/commands/StringSplit.htm

HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: RegExMatch - AutoHotkey_2.0-a077-c2bb552

07 May 2017, 09:52

Or use RegExCallOut:

Code: Select all

str:= "8 9 7"
pos:= RegExMatch(str, "(\d)(?C:RegExCallOut)")
RegExCallOut(Match, CalloutNumber, FoundPos, Haystack, NeedleRegEx){
  MsgBox(FoundPos "|" match.count() "|" match.1)
  return true
}
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: RegExMatch - AutoHotkey_2.0-a077-c2bb552

07 May 2017, 15:15

Hm
Actually I need to get first 2 float from the next string: Print size = 84.7 x 60.3 cm; 33.33 x 23.74 inches
the code that I use:

Code: Select all

if RegExMatch(A_LoopReadLine, "(\d+.\d+ [x] \d+.\d+)", V) {
	V:= strSplit(V[1], "x", A_Space)
	fileAppend(format("{:6d}|{:6d}|{:s}`n", V[1]*10, V[2]*10, fnm), Result)
}
RegExMatch return 84.7 x 60.3 then I split it by x and omit spaces.
So I get to find method that will extract all floats in one RegExMatch command instead of RegExMatch and StrSplit.
AHKv2.0 alpha forever.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: RegExMatch - AutoHotkey_2.0-a077-c2bb552

07 May 2017, 16:03

That's simple. Put the \ds into seperate brackets
Recommends AHK Studio
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: RegExMatch - AutoHotkey_2.0-a077-c2bb552

07 May 2017, 17:07

OK - I think there may be have and other solutions but I wrote RegExNMatch function that return first N Matches.

Code: Select all

;--------------------------------------------------------------------------------------
;RegExNMatch(Haystack, NeedleRegEx [, OutputVar, Numbers:= 1, StartingPosition:= 1])
;	find first Numbers matches of NeedleRegEx in Haystack from StartingPosition
;	return numbers of actually found NeedleRegEx
;
RegExNMatch(Haystack, NeedleRegEx, ByRef OutputVar:="", Numbers:= 1, StartingPosition:=1) {
	OutputVar:=[] ;create OutputVar object
	loop Numbers  ; 
		if StartingPosition:= RegExMatch(Haystack, NeedleRegEx, v, StartingPosition) {
			OutputVar.push(v[1])
			StartingPosition += v.len(1)
		} else break
	return OutputVar.length()	
} ;------------------------------------------------------------------------------------
I keep variable names and order but Numbers.
Usage

Code: Select all

str:= "Print size = 84.7 x 60.3 cm; 33.33 x 23.74 inches"

if RegExNMatch(str, "(\d*\.\d*)", V, 2) ;extract first 2 floats
loop V.length()
	msgBOX(V[A_Index])
Thanks to all.
AHKv2.0 alpha forever.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: -Garfle-, Draken, JPMuir and 57 guests