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 "Object Match" which should be "Match Object" per Lexikos comments below, thanks