Is it possible to use RegEx Delimiters with LoopParse or StrSplit()/StringSplit?

Get help with using AutoHotkey and its commands and hotkeys
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Is it possible to use RegEx Delimiters with LoopParse or StrSplit()/StringSplit?

06 Nov 2018, 19:36

Hi,

I'm currently using the function below in order to use regex delimiters to split strings, but, since I don't want to use too many extra non-built-in functions in my scripts, is it possible to do the same by using LoopParse or StrSplit()/StringSplit?

Code: Select all

Colors := "red,green,blue,   ,_ ,_   ,blue,"

Delimiter := ",(?!_)"		;Delimiter is any "," without "_" at its right!


	;______ Alternatuve 1 _________


StringSplit(Colors, Delimiter)

loop, % StringSplit([0])
Out .= "Color number " A_Index " is ''" StringSplit([a_index]) "'' ! `r`n"
msgbox, % "Alternative 1 `r`n" Out


	;______ Alternative 2 (for those who like object return values) ______


ColorArray := StringSplit(Colors, Delimiter)

Loop, % ColorArray.Length()
Result .= "Color number " A_Index " is ''" ColorArray[A_Index] "'' ! `r`n"
MsgBox, % "Alternative 2 `r`n" Result


StringSplit(String, Dlm := "`r`n")	;_____________ StringSplit(Function) - v1.0 _________________
{
Static StrpSplt

	if IsObject(String)
	{
	if (String[1] = 0)
	return, StrpSplt.Length()

	Key := String[1]

	return, StrpSplt[Key]
	}

StrpSplt := []

	StringLength := 0
	PosFound := 1
	loop
	{
	PosFound := RegExMatch(String, "s)(.*?)((" Dlm ")|$)", Match, PosFound + StringLength)

		if (PosFound = 0 or PosFound == "")
		break
		else
		{
		StringLength := StrLen(Match)

		StrpSplt[a_index] := Match1

		if (Match2 == "")	;if blank, means that end of the string "$" was found
		break
		}
	}
	
return, StrpSplt
}

Code: Select all

Colors := "red,green,blue,   ,_ ,_   ,blue,"

Delimiter := ",(?!_)"		;Delimiter is any "," without "_" at its right!


	;______ Alternatuve 1 _________


StringSplit(Colors, Delimiter)

loop, % StringSplit([0])
Out .= "Color number " A_Index " is ''" StringSplit([a_index]) "'' ! `r`n"
msgbox, % "Alternative 1 `r`n" Out


	;______ Alternative 2 (for those who like object return values) ______


ColorArray := StringSplit(Colors, Delimiter)

Loop, % ColorArray.Length()
Result .= "Color number " A_Index " is ''" ColorArray[A_Index] "'' ! `r`n"
MsgBox, % "Alternative 2 `r`n" Result


StringSplit(String, Dlm := "`r`n")	;_____________ StringSplit(Function) - v1.0 _________________
{
Static StrpSplt

	if IsObject(String)
	{
	if (String[1] = 0)
	return, StrpSplt.Length()

	Key := String[1]

	return, StrpSplt[Key]
	}

StrpSplt := []

	StringLength := 0
	PosFound := 1
	loop
	{
	PosFound := RegExMatch(String, "s)(.*?)((" Dlm ")|$)", Match, PosFound + StringLength)

		if (PosFound = 0 or PosFound == "")
		break
		else
		{
		StringLength := StrLen(Match)

		StrpSplt[a_index] := Match1

		if (Match2 == "")	;if blank, means that end of the string "$" was found
		break
		}
	}
	
return, StrpSplt
}
Last edited by User on 06 Nov 2018, 20:01, edited 3 times in total.
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is it possible to use RegEx Delimiters with LoopParse or StrSplit()/StringSplit?

06 Nov 2018, 19:38

Oh My GOD, What a weird code box is this? Is this a joke or something?

I'm using simplicity theme!
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is it possible to use RegEx Delimiters with LoopParse or StrSplit()/StringSplit?

06 Nov 2018, 19:56

Colors := "red,green,blue, ,_ ,_ ,blue,"

Delimiter := ",(?!_)" ;Delimiter is any "," without "_" at its right!


;______ Alternatuve 1 _________


StringSplit(Colors, Delimiter)

loop, % StringSplit([0])
Out .= "Color number " A_Index " is ''" StringSplit([a_index]) "'' ! `r`n"
msgbox, % "Alternative 1 `r`n" Out


;______ Alternative 2 (for those who like object return values) ______


ColorArray := StringSplit(Colors, Delimiter)

Loop, % ColorArray.Length()
Result .= "Color number " A_Index " is ''" ColorArray[A_Index] "'' ! `r`n"
MsgBox, % "Alternative 2 `r`n" Result


StringSplit(String, Dlm := "`r`n") ;_____________ StringSplit(Function) - v1.0 _________________
{
Static StrpSplt

if IsObject(String)
{
if (String[1] = 0)
return, StrpSplt.Length()

Key := String[1]

return, StrpSplt[Key]
}

StrpSplt := []

StringLength := 0
PosFound := 1
loop
{
PosFound := RegExMatch(String, "s)(.*?)((" Dlm ")|$)", Match, PosFound + StringLength)

if (PosFound = 0 or PosFound == "")
break
else
{
StringLength := StrLen(Match)

StrpSplt[a_index] := Match1

if (Match2 == "") ;if blank, means that end of the string "$" was found
break
}
}

return, StrpSplt
}
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is it possible to use RegEx Delimiters with LoopParse or StrSplit()/StringSplit?

06 Nov 2018, 19:58

Code: Select all

Colors := "red,green,blue,   ,_ ,_   ,blue,"

Delimiter := ",(?!_)"		;Delimiter is any "," without "_" at its right!


	;______ Alternatuve 1 _________


StringSplit(Colors, Delimiter)

loop, % StringSplit([0])
Out .= "Color number " A_Index " is ''" StringSplit([a_index]) "'' ! `r`n"
msgbox, % "Alternative 1 `r`n" Out


	;______ Alternative 2 (for those who like object return values) ______


ColorArray := StringSplit(Colors, Delimiter)

Loop, % ColorArray.Length()
Result .= "Color number " A_Index " is ''" ColorArray[A_Index] "'' ! `r`n"
MsgBox, % "Alternative 2 `r`n" Result


StringSplit(String, Dlm := "`r`n")	;_____________ StringSplit(Function) - v1.0 _________________
{
Static StrpSplt

	if IsObject(String)
	{
	if (String[1] = 0)
	return, StrpSplt.Length()

	Key := String[1]

	return, StrpSplt[Key]
	}

StrpSplt := []

	StringLength := 0
	PosFound := 1
	loop
	{
	PosFound := RegExMatch(String, "s)(.*?)((" Dlm ")|$)", Match, PosFound + StringLength)

		if (PosFound = 0 or PosFound == "")
		break
		else
		{
		StringLength := StrLen(Match)

		StrpSplt[a_index] := Match1

		if (Match2 == "")	;if blank, means that end of the string "$" was found
		break
		}
	}
	
return, StrpSplt
}
User avatar
TLM
Posts: 1418
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: Is it possible to use RegEx Delimiters with LoopParse or StrSplit()/StringSplit?

06 Nov 2018, 20:21

User wrote:
06 Nov 2018, 19:58
Is it possible to use RegEx Delimiters with LoopParse or StrSplit()/StringSplit?
Not directly with those functions and or commands but you can parse a string with a RegEx loop

Code: Select all

Colors := "red,green,blue,   ,_ ,_   ,blue,"

While Pos:=RegExMatch( Colors, "(?<match>\w+)`,", _, !Pos?1:(StrLen(_)+Pos))
	msgbox % _match
for direct help, tweet at me: https://twitter.com/CW_DTech
User
Posts: 407
Joined: 26 Jun 2017, 08:12

Re: Is it possible to use RegEx Delimiters with LoopParse or StrSplit()/StringSplit?

06 Nov 2018, 22:00

TLM wrote:
06 Nov 2018, 20:21
Thanks for your code, but I think it's not suitable for splitting strings (it is not even suitable for the colors example above)!

Not directly with those functions and or commands ...
So I will keep using "StringSplit()" function from the example above, it allows regex delimiters! (maybe one day regex delimiters will be implemented in built-in functions\commands)

By the way, is the email notifications disabled? I am not receiving them!

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], DRocks, flyingDman, johnqflorida and 50 guests