Regex to a string 123123 to give 123

Hi,

I want to use Regex to a string to give only 123 from 123123, this could be any number any amount. For example 43564356, 1212, and so on… Sometimes this value is just 1456 then the regex should do nothing, but other times its 14561456 how do i get the 1456 value only?

I have used this website “https://regex101.com/”, its working here but i have problems to convert it to uipath.

Hi,

Can you try the following expression?

m = System.Text.RegularExpressions.Regex.Match(yourString,"(\d+)(\1)")

then

m.Groups(1).Value

Note: m is System.Text.RegularExpressions.Regex.Match.

img20220110-a4

Regards,

Thanks, this solves the problem. :slight_smile:

1 Like

But when i have the value only like 1456, theres no output.

Hi,

What about using just “\d+”, if the above expression returns no match?
Or do you need single regex pattern?

Regards,

This value is used in a “if” statement, it checks if this value (1456) is equal to another value (1456) im getting from an excel file. So i need a single regex pattern.

Hi,

Alright. Can you try the following expression?

System.Text.RegularExpressions.Regex.Match(yourString,"(\d+)(?=\1)|(\d+)").Value

Regards,

Still doesn’t match when I have just 1456.

Hi,

Seems strange. In my environment, it works as the following image.

img20220110-a5

Regards,

I found out that, yourString.Groups(0).Value gives the values I want. Either if it is 1456 or 14561456. So the expression is correct.

Thanks then I have the answer that I needed.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.