Convert Regex Result to String Variable

Hi

I have used Regex to get part of a filename but I can’t convert to a string variable, can anyone help please.

Thanks
Raychel

@raychel.hall

If you are using match then use .value to get the output

If using matches …then you can use index to get each output(0).value

Or use a for loop and sent the output as in argument for for loop and change type argument to match

And then inside loop use currentitem.value

Hope thsi helps

Cheers

Thanks for your reply Anil :slight_smile:

I have used Matches, please can you show me what the assign would look like.

Thanks

Raychel

Hi @raychel.hall ,

If it’s a Single Value match that you would require, then you can use the Match Method instead of the Matches Activity or Matches method.

System.Text.RegularExpressions.Regex.Match(yourInputVar,"Your Regex").Value.ToString
1 Like

Hi @supermanPunch

The return value will be more than one word.

Thanks

Raychel

Hi @raychel.hall

You can replace the Matches activity with the Assign activity and modify the expression as shown below:

yourOutputVar = System.Text.RegularExpressions.Regex.Match(YourString, "YourRegex").ToString();

Can you share the sample input and output?

Regards
Gokul

@raychel.hall

Say output is stored in output variable

Then use for loop and give output and change type argument to match

Now inside use assign with

Requiredword = currentitem.value

For every iteration new value will be assigned to requiredword variable which is of string type

If you mnow how many words and need a specific word for suppose need second word use output(1).Value index starts from 0 so for second value index is 1

Hope this helps

To know how many matches are found use output.count

If you want to see all words at once then

String.Join(",",output.Cast(Of Regex.Match).Select(function(x) x.Value))

Cheers

@raychel.hall ,

In that case we would need to know how are these words connected. Do we need to append all the matched values/words together into a Single variable or does each matched word need to be assigned to different variables ?

Hi

My input is C:\Users\*******\OneDrive - **** plc\UI Path processes\Key Words Process - One Excel\Input Files\SCC 2030 Net Zero Carbon Programme 002

I need to get the words after the last \ in one variable.

Thanks

Raychel

@raychel.hall

If you need that then use like this it would give all the words

Str.Split({"\"},StringSplitOptions.None).Last

Edit: thanks @Gokul001

Cheers

Hi @raychel.hall

Try with Split expression

Split("C:\Users\*******\OneDrive - **** plc\UI Path processes\Key Words Process - One Excel\Input Files\SCC 2030 Net Zero Carbon Programme 002","\").Last

Regards
Gokul

Little modification in the syntax

Str.Split({"\"},StringSplitOptions.None).Last

Regards
Gokul

@raychel.hall ,

We also see that it is a Path, and for such file paths, we could use the below Expression as well :

Path.GetFileName("C:\Users\*******\OneDrive - **** plc\UI Path processes\Key Words Process - One Excel\Input Files\SCC 2030 Net Zero Carbon Programme 002")

image

We don’t see the extension mentioned, but we could also remove the Extension with the GetFileNameWithoutExtension() method if at all the extension is to be removed.