String to Array using Regular Expressions

I know it is possible to split a string to an array using this:

myArray = someText.split(",")

My problem is that I have a few of delimiters. These delimiters can be defined and are not static.
What I did before was every time I want to get my Array, I changed all the desired delimiters into a chr(0). After that I got my array by splitting the string by the chr(0).

I was looking to a more efficient way of doing that. I thought about Regular Expressions. But that is still a bit gibberish to me. I simply don’t know where to start.

Any ideas?

In this example, let’s say you want to split by either “,” or “;”.

dim rx as new RegEx
rx.SearchPattern = "(?Us)(.*)(?:[,;]|\\z)"

dim splits() as string
dim match as RegExMatch = rx.Search( s )
while match isa RegExMatch
  splits.Append match.SubExpressionString( 1 )
  match = rx.Search
wend

return splits

The breakdown of the pattern:

(?Us) = switches Ungreedy (not greedy), dot matches newline
.* = match anything until…
([,;]|\z) = one of the delimiters or the end of the document

[quote=427252:@Kem Tekinay]In this example, let’s say you want to split by either “,” or “;”.

dim rx as new RegEx
rx.SearchPattern = "(?Us)(.*)(?:[,;]|\\z)"

dim splits() as string
dim match as RegExMatch = rx.Search( s )
while match isa RegExMatch
  splits.Append match.SubExpressionString( 1 )
  match = rx.Search
wend

return splits

The breakdown of the pattern:

(?Us) = switches Ungreedy (not greedy), dot matches newline
.* = match anything until…
([,;]|\z) = one of the delimiters or the end of the document[/quote]

So, if I want to add a delimiter I place that within the square brackets? (After b
And what if I want to add an EndOfLine to the delimiter list?

@Kem Tekinay

I took the liberty of translating your explanation into Klingon:

[quote]chay’ ‘oH QIj SoH pa’ tlhIngan?

(? maHvaD) boch chemvaH Ho’Du’ ungreedy = (wej qur), newline qul naQmey dot
. * qul naQmey = vay’ until.
([,;]| \z) delimiters wa’ pagh bertlham document =[/quote]

:stuck_out_tongue:

HAHA!

[quote=427253:@Edwin van den Akker]So, if I want to add a delimiter I place that within the square brackets? (After b
And what if I want to add an EndOfLine to the delimiter list?[/quote]

Yes unless your delimiter is more than one character. For example, if you wanted “,”, “;”, or “–”, you’d write it as:

[,;]|--|\\z

Adding EOL would be:

(Editing this to something better)

[,;]|\\R|\\z

\R represents any EOL sequence.

One note about this: On a larger document, this is going to be slow. Unfortunately, this is a byproduct of Xojo’s RegEx implementation. MBS’s regex is significantly faster.

I’m actually aware of that. I have a license for the MBS plugins. So, I will use that one instead of Xojo’s build in one.

Thanks a lot Kem!
The world is worthless without your endless knowledge, haha :smiley:

Fact check: true.

In Klingon please

:smiley: