Example, RegEx Array vs Pseudo Array

Helpful script writing tricks and HowTo's
tripshack
Posts: 8
Joined: 26 Nov 2014, 23:21

Example, RegEx Array vs Pseudo Array

02 Dec 2014, 23:43

I wrote this example while learning about RegEx and how it works in AHK.
The Match Object vs Pseudo array output was very confusing until I saw what was going on.
This may be mentioned in the docs, but not exactly stated outright.


Code: Select all

/*
        20141202 
	RegExMatch example
	Shows output types and basic RegEx usage

	This RegEx Needle looks for 2 alpha characters and then 1-5 numbers

		Needle = "iO)^([a-z]{2})([0-9]{1,5})"

		Breakdown:

		iO)        Options:  i for not case sensitive,  O to return as Match Object[]
		^          Match from start of line
		[a-z]{2}   Match exactly 2 letters in a row.
		()         Uing () here will return match as next element in array

		([0-9]{1-5}) Return match of 1-5 numbers (only if right after the a-z)


	abc123 returns nothing because 3 letters
	de123  returns { de, 123 }
	fg3    returns { fg, 3 }
	h4444  returns nothing, only 1 letter
	ij     returns nothing, no #s
	66     returns nothing, no letters

	The first Gosub ShowKeyx uses "O" option which...
		Returns matches in Mats[1] and Mats[2]
		Returns count in Mats.Count

	The second RegEx does not use "O" and...
		Returns matches in Mats1, Mats2
		No count is returned.  Mats0 not used.

	MatAt will always return 1 due to the ^

	Today I learned:
		Match.Count is case sensitive. (the C in count has to be upper case)
		Match.Count() also works.
		RegEx returns (pseudo) array even if "O" not specified
		Pseudo arrays made by system don't always have a count at 0.

*/

nl := "`n"   ; making life easy
tb := a_tab
sp := a_space

keyx := "abc123,de222,fg3,h444,ij,66"  ;parse & regex this

Needle := "iO)^([a-z]{2})([0-9]{1,5})"
m := "RegExMatch Match Object (using O)" nl " Needle=" Needle nl nl  ;for msgbox output
Gosub ShowKeyx

Needle := "i)^([a-z]{2})([0-9]{1,5})"
m := "RegExMatch Pseudo array (no O)" nl " Needle=" Needle nl nl  ;for msgbox output
Gosub ShowKeyx

exitapp
esc::exitapp

ShowKeyx:
	loop,parse,keyx,`,
	{
		key := A_LoopField
		MatAt := RegExMatch(key,Needle,Mats)

		m .= "Key=" key "   Mat0=" Mats0 " Mats.Count=" Mats.Count  nl nl

		loop, 2
			m .= tb "Mats" a_index " = " Mats%a_index% tb "   Mats[" a_index "]=" Mats[a_index] nl

		m .= nl
	}
	msgbox % m
return
*Corrected references to "Array" which should be "Object Match" per Lexikos comments below, thanks.
**Corrected references to "Object Match" which should be "Match Object" per Lexikos comments below, thanks :)
Last edited by tripshack on 22 Dec 2014, 23:00, edited 3 times in total.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Example, RegEx Array vs Pseudo Array

22 Dec 2014, 01:59

It's not an array. It's a Match Object.
Mode 3 (match object) [v1.1.05+]: If a capital O is present in the RegEx's options -- such as "O)abc.*123" -- a match object is stored in UnquotedOutputVar. This object can be used to retrieve the position, length and value of the overall match and of each captured subpattern, if present.
Match Object
If a capital O is present in the RegEx's options, a match object is stored in UnquotedOutputVar. This object has the following properties:

Match.Pos(N): Returns the position of the overall match or a captured subpattern.
Match.Len(N): Returns the length of the overall match or a captured subpattern.
Match.Value(N): Returns the overall match or a captured subpattern.
Match.Name(N): Returns the name of the given subpattern, if it has one.
Match.Count(): Returns the overall number of subpatterns.
Match.Mark(): Returns the NAME of the last encountered (*MARK:NAME), when applicable.

Match[N]: If N is 0 or a valid subpattern number or name, this is equivalent to Match.Value(N). Otherwise, N can be the name of one of the above properties. For example, Match["Pos"] and Match.Pos are equivalent to Match.Pos() unless a subpattern named "Pos" exists, in which case they are equivalent to Match.Value("Pos").

Match.N: Same as above, except that N is an unquoted name or number.

For all of the above properties, N can be any of the following:
  • 0 for the overall match.
  • The number of a subpattern, even one that also has a name.
  • The name of a subpattern.
Brackets [] may be used in place of parentheses () if N is specified.
tripshack
Posts: 8
Joined: 26 Nov 2014, 23:21

Re: Example, RegEx Array vs Pseudo Array

22 Dec 2014, 13:03

lexikos wrote:It's not an array. It's a Match Object.
Thanks. Match Object is the correct term to use. I'll fix that in the original post.
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: Example, RegEx Array vs Pseudo Array

22 Dec 2014, 20:56

Object Match != Match Object. ;)
tripshack
Posts: 8
Joined: 26 Nov 2014, 23:21

Re: Example, RegEx Array vs Pseudo Array

22 Dec 2014, 23:02

lexikos wrote:Object Match != Match Object. ;)
Oops. Fixed.

Return to “Tutorials (v1)”

Who is online

Users browsing this forum: No registered users and 56 guests