RegEx: edit RegEx match object

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6647
Joined: 19 Dec 2016, 01:58
Location: UK

RegEx: edit RegEx match object

30 Nov 2017, 05:48

- I was converting RegExMatch code from variable mode to object mode.
- It appears that you cannot edit this array in the normal way.
- Is there a way to edit it?
- Otherwise I'll probably clone the numeric keys to an array or to variables, because sometimes I want to edit some of the values after retrieving the matches.
- Also, does it say anywhere that it's not a standard AutoHotkey array? Thanks.
- Btw, I suppose this could have been made as a standard array, is there some special optimisation involved? Is this done with large strings in mind, where having things read-only is advantageous.

Code: Select all

q::
RegExMatch("abc", "O)(.)(.)(.)", o)
MsgBox, % o.1 " " o.2 " " o.3 ;a b c
o.1 := "d"
MsgBox, % o.1 ;a
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 3891
Joined: 17 Jul 2016, 01:02
Contact:

Re: RegEx: edit RegEx match object

30 Nov 2017, 07:09

It is a match object, as per the documentation,
This object has the following properties: [...]
What you see is what you get, you cannot edit, clone(), enumerate...
I suppose this could have been made as a standard array, is there some special optimisation involved?
It is an interesting question. :wave:

Cheers.
User avatar
jeeswg
Posts: 6647
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RegEx: edit RegEx match object

30 Nov 2017, 12:40

- Haha cheers. Yes, when I read 'match object', that half looks like a special type of object, half looks like just a normal object with match info with a certain hierarchical structure. But then, if I were to say 'match variable', or 'match pseudo-array', it's clearer what that would mean.
- It would be potentially interesting to 'enumerate' everything (by using the info that is available). Although it might not be too hard, I haven't needed to do it yet, I'll try it shortly.
- Did it say anywhere that you can't enumerate or write to the object? It's quite the mysterious object. Btw I meant 'clone' in a generic sense, to just create a standard array, and copy across selected keys.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 3891
Joined: 17 Jul 2016, 01:02
Contact:

Re: RegEx: edit RegEx match object

30 Nov 2017, 18:17

I do not think it is explicitly stated that you cannot modify it. v2 docs says you cannot use for loop, v1 doesn't it seems.
User avatar
jeeswg
Posts: 6647
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RegEx: edit RegEx match object

04 Jan 2018, 23:03

Here's a function that approximately clones a RegEx match object. I did this more to understand how the match object works, than as something I would use day-to-day.

Code: Select all

JEE_RegExObjClone(oMatch)
{
	static vList := "Pos,Len,Value,Name,Count,Mark"
	oArray := {Pos:{},Len:{},Value:{},Name:{},Count:{},Mark:{}}
	oName := []
	oArray.Count := oMatch.Count()
	oArray.Mark := oMatch.Mark()
	Loop, % oMatch.Count()+1
	{
		vIndex := A_Index-1
		oArray[vIndex] := oMatch[vIndex]
		Loop, Parse, vList, % ","
			oArray[A_LoopField, vIndex] := oMatch[A_LoopField, vIndex]
		if ((vName := oMatch.Name(vIndex)) = "")
			continue
		oArray[vName] := oMatch[vName]
		Loop, Parse, vList, % ","
			oArray[A_LoopField, vName] := oMatch[A_LoopField, vName]
	}
	return oArray
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6647
Joined: 19 Dec 2016, 01:58
Location: UK

Re: RegEx: edit RegEx match object

04 Jan 2018, 23:21

Here is a more practical example re. partially cloning a match object. Re. converting a date from one format to another. You split the date into parts, and want to convert the month text to a number.

Code: Select all

q:: ;convert dates (partially clone match objects)
vList := "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
oMonths := {}
Loop, Parse, vList, % ","
	oMonths[A_LoopField] := A_Index

RegExMatch("25 Dec 2017", "O)(?P<Day>\d+) (?P<Month>[A-Za-z]+) (?P<Year>\d+)", oMatch)
MsgBox, % oMatch.3 " " oMatch.2 " " oMatch.1
MsgBox, % oMatch.Year " " oMatch.Month " " oMatch.Day

;create an array that clones some of the match object keys
oArray := {}
Loop, % oMatch.Count()
{
	oArray[A_Index] := oMatch[A_Index]
	oArray[oMatch.Name(A_Index)] := oMatch[oMatch.Name(A_Index)]
	MsgBox, % oMatch.Name(A_Index) ": " oMatch[oMatch.Name(A_Index)]
}
oArray.2 := oMonths[oArray.2]
oArray.Month := oMonths[oArray.Month]
MsgBox, % oArray.3 " " oArray.2 " " oArray.1
MsgBox, % oArray.Year " " oArray.Month " " oArray.Day

;create a pseudo-array that clones some of the match object keys
Loop, % oMatch.Count()
{
	vTemp := oMatch.Name(A_Index)
	v%A_Index% := oMatch[A_Index]
	v%vTemp% := oMatch[oMatch.Name(A_Index)]
}
v2 := oMonths[oMatch.2]
vMonth := oMonths[oMatch.Month]
MsgBox, % v3 " " v2 " " v1
MsgBox, % vYear " " vMonth " " vDay
return
Link:
RegExMatch
https://autohotkey.com/docs/commands/Re ... atchObject
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: DannyhelMont, Google [Bot] and 137 guests