iregexmatch split parameter -----"a,g(b,e,f),k[c,f],d" Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
cgx5871
Posts: 315
Joined: 26 Jul 2018, 14:02

iregexmatch split parameter -----"a,g(b,e,f),k[c,f],d"

Post by cgx5871 » 03 Jun 2023, 14:04

Code: Select all

#SingleInstance
input_string:="a,g(b,e,f),k[c,f],d,..........."
;~ regex_pattern := "(\([^()]*\)|[^,]+)"
regex_pattern := "([^,]+)"

if RegExMatch(input_string, regex_pattern, &matches) {
	MsgBox matches[1]		;______a
	MsgBox matches[2]		;______g(b,e,f)
	MsgBox matches[3]		;______k[c,f]
	MsgBox matches[4]		;______d
	;~ ......
}
how to split parameter by `, commas

User avatar
mikeyww
Posts: 26884
Joined: 09 Sep 2014, 18:38

Re: iregexmatch split parameter -----"a,g(b,e,f),k[c,f],d"

Post by mikeyww » 03 Jun 2023, 14:29

Code: Select all

#Requires AutoHotkey v2.0
input := 'a,g(b,e,f),k[c,f],d'
regex := '[^,]([([].+?[)\]])?'
pos   := 0
While pos := RegExMatch(input, regex, &m, pos ? pos + StrLen(m[]) : 1)
 MsgBox m[]

cgx5871
Posts: 315
Joined: 26 Jul 2018, 14:02

Re: iregexmatch split parameter -----"a,g(b,e,f),k[c,f],d"

Post by cgx5871 » 03 Jun 2023, 14:41

@mikeyww
Is there a way to capture to M[1],M[2]......

User avatar
mikeyww
Posts: 26884
Joined: 09 Sep 2014, 18:38

Re: iregexmatch split parameter -----"a,g(b,e,f),k[c,f],d"  Topic is solved

Post by mikeyww » 03 Jun 2023, 14:44

Code: Select all

#Requires AutoHotkey v2.0
input := 'a,g(b,e,f),k[c,f],d'
m     := split(input)
MsgBox m[1]
MsgBox m[2]
MsgBox m[3]
MsgBox m[4]

split(str) {
 Static regex := '[^,]([([].+?[)\]])?'
 pos := 0, j := []
 While pos := RegExMatch(str, regex, &m, pos ? pos + StrLen(m[]) : 1)
  j.Push m[]
 Return j
}

cgx5871
Posts: 315
Joined: 26 Jul 2018, 14:02

Re: iregexmatch split parameter -----"a,g(b,e,f),k[c,f],d"

Post by cgx5871 » 04 Jun 2023, 07:36

@mikeyww
input := 'a,rOffset(aTitle,i).address,g(b,e,f),k[c,f],d'
Last edited by cgx5871 on 04 Jun 2023, 07:44, edited 2 times in total.

User avatar
mikeyww
Posts: 26884
Joined: 09 Sep 2014, 18:38

Re: iregexmatch split parameter -----"a,g(b,e,f),k[c,f],d"

Post by mikeyww » 04 Jun 2023, 07:42

We have entered the "That works, but what if...?" part of the regex tutorial!

Like so many types of coding, use of regex is designed to match the situation at hand. One needs to identify and describe all possible formats of text that should match the expression. We otherwise enter an endless loop here. Feel free to adjust the regex to match what you need. If you cannot delineate, identify, or describe the formats, then it probably cannot be done. Providing a single example of text, as done in your initial post, is typically insufficient, because that example can be described in many different ways, most of which would not fit the need at hand.

The regex that I wrote looks for a single character, [^,], that is not a comma. Where needed, + can be applied to look for one or more such characters.

Explained: Commonly used symbols and syntax

cgx5871
Posts: 315
Joined: 26 Jul 2018, 14:02

Re: iregexmatch split parameter -----"a,g(b,e,f),k[c,f],d"

Post by cgx5871 » 04 Jun 2023, 10:14

@mikeyww
Thank you so much
Relearned regular
But this is so easy to forget.

seems fix it

Code: Select all

text := "rOffset(aTitle,i).address,a,g(b,e,f),k[c,f],d"
pattern := ",(?![^\(\[]*[\]\)])"
result := RegExReplace(text, pattern, "@")
m :=StrSplit(result,"@")
MsgBox m[1]

Post Reply

Return to “Ask for Help (v2)”