Add separator using the regex

Hi there,

I have a string variable that I want to split into a string array using the regex. The problem is the whole string is together and there are no separators.
I want to insert separators so that I can later split the strings into an array. How should I proceed?

Could you provide an example of the string? Is there any pattern you can use to split the string?

for example a string like this:
"text1text2text3"

I want to insert a separator between each “text(nr)” and thus split the string into an array afterwards with string.split.

I’ve tried to split it with regex.split. It does split but it adds 3 empty strings to the array.
Used command: arraystring = regex.split(stringinput, “\w{4}\d{1}”)

I’ve also checked the variable type, but it’s set correctly.

1 Like

@Keemper
maybe you can work with a regex:
grafik

and insert the seperator with the help of replace:
grafik

so have a look to the regex.replace method or use following activity:
grafik

the last unneeded seperator we can easy remove and correct

1 Like

Hi,

We can directly get an array from the string as the following.

strVar ="text1text2text3"
arrString = System.Text.RegularExpressions.Regex.Matches(strVar,"[A-Za-z]+\d+").Cast(Of System.Text.RegularExpressions.Match).Select(Function(m) m.Value).ToArray

Regards,

1 Like

So many possible solutions! :grinning:

A third option would be to put your regex pattern in (?=...). The first item will be empty, so remove it with Skip().

myArray = Regex.Split("text1text2text3", "(?=\w{4}\d{1})").Skip(1).ToArray

image

1 Like

Hey @Keemper

As @ptrobot said, so many options.

I like @ptrobot and @Yoichi’s solution in this post.

It also depends on the length of the word and digits. Will they change? Something to consider.

Good luck :slight_smile:

1 Like

Yeah, the length of the words (digits) do change.

@ptrobot
@Yoichi
@ppr

FYI - The word length does change :slight_smile:

@Keemper - How does this pattern work

As long as there are numbers between each word it will match all the words…

Also be sure to check out my Regex MegaPost :slight_smile:

Regex help tutorial MEGAPOST – Making your first Regex post, Reusable Regex Patterns, Regex Troubleshooting, Sample Workflow and more

1 Like

Thanks @Steven_McKeering! My solution was just to put any regular expression in a positive lookahead and use it with Regex.Split() and Skip(). Since @Keemper already made a correct regular expression (for the sample text), I’m sure he/she can adjust it to include words and digits of variable length.

Update: It turns out that it was not that easy to use variable length with positive lookahead. So Yoichi’s solution is the best solution so far.

1 Like

@Keemper
based on my approach from above:

Regex.Replace(strDemo,"\d+","$0#").Split({"#"}, StringSplitOptions.RemoveEmptyEntries)