How to try to extract the first 2 digits from a string?

Im trying to extract the first 2 digit from a string
example: The 11 Day of APRIL 2023
I want to extract only “11” is there any solution that i can do that?

Hi @lean0919 ,

You could try using the below Expression :

Regex.Match("The 11 Day of APRIL 2023","\d+").Value.Trim

image

What if the text is like The date 10112023 today
and i only wan to get the first 2 numbers

Hi @lean0919 ,

Try the following in an assign activity:

System.text.regularExpressions.Regex.Match("Your String","(?<=The\s)(\d+)(?=\sDay)").Tostring.Trim

Regards,

What if the text is like “The date 10112023 today”
and i only wan to get the first 2 numbers

Hi @lean0919

You can use the following RegEx pattern to extract the date part from the given string:

\b\d{2}\b

image

Syntax:

System.Text.RegularExpressions.Regex.Match(yourString,"\b\d{2}\b").Value

Hope this helps,
Best Regards.

1 Like

@lean0919

Well, in that case, you can use this:

System.Text.regularExpressions.Regex.Match(yourString,"\d{2}").Groups(0).ToString

Edit: Output -

Hope this helps,
Best Regards.

@lean0919 ,

The Regex Pattern can be adjusted to \d{2} as already provided above.

Regex.Match("The date 10112023 today","\d{2}").Value.Trim

image

Without the Group too, we should be able to fetch it.

2 Likes

Thank you!

1 Like

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