Split string into array using regular expression (variable) delimiters

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Split string into array using regular expression (variable) delimiters

08 May 2021, 00:13

Hi everyone:

I have a string such as this one:

String:= <TAG1G><TAG3G>Conjunto de motor de bomba<TAG3G><TAG2G>317-8494<TAG2G><TAG1G>

I need to create a array where the first element of the array is <TAG1G>, the second is <TAG3G>, the third is Conjunto de motor de bomba, the fourth is <TAG3G>, the fifth is <TAG2G>, the sixth is 317-8494, the seventh is <TAG2G> and the eighth is <TAG1G>.

Of course, another string might be "This is a <TAG1G>BLAST!<TAG1G>". You get the drill, I need to split the string using <TAG<number>G> as delimiters, but contrary to what StrSplit does, I need the delimiters to be part of the array. And they might be <TAG1G>, <TAG2G>, etc. Also, the number of array elements is not fixed. And the number of tags occurrences such as <TAG1G>, <TAG8G>, etc. varies from zero to any number (there may be strings without any tags, or strings with several).
The reason is that I need to be able to operate with elements of the array independently (in this case, for example, I would need to apply a change to Title Format to the array element "Conjunto de motor de bomba" and that's the only way I can think to do it, that is, splitting the string in array elements, finding the one I need to modify, modifying it and then reconstructing the array.

The thing is that I only know how to use Loop, Parse or StrSplit, and I have no idea how to expand those. I have an inkling that perhaps RegexMatch() must be involved somehow, but it's more complicated than I thought...
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Split string into array using regular expression (variable) delimiters

08 May 2021, 01:32

Would this help?:

Code: Select all

var = This is a <TAG1G><TAG3G>Conjunto de motor de bomba<TAG3G><TAG2G>317-8494<TAG2G><TAG1G>

a := strsplit(var, [">", "<"])
b := []
for x,y in a
	if instr(y,"tag")
		b.push("<" y ">")
	else if y
		b.push(y)

for t,u in b
	msgbox % u
	
14.3 & 1.3.7
Nixcalo
Posts: 116
Joined: 06 Feb 2018, 04:24

Re: Split string into array using regular expression (variable) delimiters

08 May 2021, 01:47

Seems amazing although I have to study it carefully. I have no idea why it works, but it definitely seems to work! I am going to study it before setting this post as "Solved"... and I have to do some testing... and I have to try to understand how you did it so beautifully and simply and ingeniously.
User avatar
Chunjee
Posts: 1422
Joined: 18 Apr 2014, 19:05
Contact:

Re: Split string into array using regular expression (variable) delimiters

08 May 2021, 01:57

https://biga-ahk.github.io/biga.ahk/#/?id=split allows for splitting with a regex separator.

Since there is nothing on the left side of "<TAG1G>" it may also require the empty values to be removed.

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

String := "<TAG1G><TAG3G>Conjunto de motor de bomba<TAG3G><TAG2G>317-8494<TAG2G><TAG1G>"
Array := A.split(String, "/<[TAG\>\<0-9]+>/")
; => ["", "Conjunto de motor de bomba", "317-8494", ""]
Array := A.compact(Array)
; => ["Conjunto de motor de bomba", "317-8494"]
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Split string into array using regular expression (variable) delimiters

08 May 2021, 02:09

You may want to replace the if instr(y,"tag") with if y~="TAG\d+G"
14.3 & 1.3.7

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, TAC109, wineguy and 298 guests