Uipath use regex to extract first string from each line

Hi. I have a paragraph, and I need to extract first word for each line. Please help.
Example: I have this paragraph, it is assign in sample_text variable:

US, John, Wa
EU, Miu, KO
JP, Got, Ko
KR, kiioo, hu

Now I need to extract US, EU, JP, KR and then concat it into a string: US_EU_JP_KR.
It will be very nice if I can do it by one line with assign activitiy, like as: result = from sample_text do ....
Please help

@voxter

Please try this

String.Join("_",System.Text.RegularExpressions.Regex.Matches(str,"^\w+",RegexOptions.MultiLine).select(function(x) x.Value))

Str is the variable containing your text…assumption is there are new lines

Cheers

Hi @voxter

Input:
image

Output:
image

Workflow
image

Expration
String.Join("_",st.Split(Environment.NewLine.ToCharArray).Select(Function(x) x.Split(","c)(0).ToString))

st = your input string value variable

Thanks,

Hope this helps you

Use a assign activity like this for a one line expression
Hope you have the variable saved as str_input

Str_output = String.Join(“_”, System.Text.RegularExpressions.Regex.Matches(str_input.ToString, “[1]+“).Select(Function(a) a.Value).ToArray())

To just explain

String.Join(“_”, your array variable) will help you to join the array of words you have found

Inside this join method we are using Regex method to get the first set of words from your input string

Regex explanation

You can try this expression in regex101.com website

Hope this explains

Cheers @voxter


  1. A-Za-z ↩︎

Hi,

Assuming you have your paragraph stored in a variable called sample_text.

Create an Assign activity and set the “To” field to your result variable, let’s call it result.

In the “Value” field of the Assign activity, you can use the following expression:

result = String.Join(“_”, sample_text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(Function(line) line.Split(","c)(0).Trim()))

@voxter Please find Solution xaml
Main.xaml (6.8 KB)

Hello @voxter
Try this regex method with Join String

String.Join("_",System.Text.RegularExpressions.Regex.Matches(YourString,"^\w+|(?<=\n)\w+"))

Hi @voxter

sample_text= "US, John, Wa
              EU, Miu, KO
              JP, Got, Ko
              KR, kiioo, hu"
result= String.Join("_",sample_text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(Function(line) line.Split(","c)(0).Trim()))

Hope it helps!!