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)

Hi. I’m new here.
Could someone help me with my problem?
the input i have: Qianhong Zhang; Ouyang Miao; Fubiao Lin; Zhongni Zhang
the output i want: Zhang, Qianhong; Miao, Ouyang; Lin, Fubiao; Zhang, Zhongni

this is how i change the order (for only 1 name):
(.?)\s([\wáâàãçéêíóôõúüÁÂÀÃÇÉÊÍÓÔÕÚÜ]+-?'?\w+.?$)

\w'?\w+.?$/gmi - LASTNAME
\w'?\w+.
?$/gmi - LASTNAME
(.?)([\wü]+-?'?\w+.?$)/gmi - FIRST AND LASTNAME
(.?)([\wáâàãçéêíóôõúüÁÂÀÃÇÉÊÍÓÔÕÚÜ]+-?'?\w+.?$)/gmi - FIRST AND LASTNAME

My question: how do i get the output i want with the input i have? how do i add the (; ) between the Names?

Have you considered to use Regex.Split() instead?

String.Join("; ",
	(From n In System.Text.RegularExpressions.Regex.Split(input, "; +")
		Let arr = System.Text.RegularExpressions.Regex.Split(n, " +")
		Select arr.Last + ", " + String.Join(" ", arr.Take(arr.Length-1))
	)
)

image

image

Thank you for your answer, but i’m not sure how to use it yet. Is there a generator for Regex.Split?

I’m currently using (regex101: firstname and lastname).

There’s no generator for Regex.Split but the substitution feature in regex101 might help you to see the result of a split if you insert the replacement value “\n”.

I still need some time to put them together in regex101, but i’m already editing the names so much faster thanks to you. :slight_smile: