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...
Split string into array using regular expression (variable) delimiters
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: Split string into array using regular expression (variable) delimiters
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
Re: Split string into array using regular expression (variable) delimiters
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.
Re: Split string into array using regular expression (variable) delimiters
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.
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"]
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: Split string into array using regular expression (variable) delimiters
You may want to replace the if instr(y,"tag") with if y~="TAG\d+G"
14.3 & 1.3.7