Read specific word from string

What should I do if I want to read only the words or characters I want from a specific string?

For example, I would like to read only the numbers 4 and 1 from the string “hanlaims 4 months 1 week data” and store them in separate variables.

HI,

How about the following?

strMonth = System.Text.RegularExpressions.Regex.Match(yourString,"\d(?=\s*month)").Value
strWeek = System.Text.RegularExpressions.Regex.Match(yourString,"\d(?=\s*week)").Value

Regards,

1 Like

Hi @Benimaru

You can use the regular expressions to extract the required data,

- Assign -> Input = "hanlaims 4 months 1 week data"

- Assign -> Variable1 = System.Text.RegularExpressions.Regex.Matches(Input.ToString, "\d+").Cast(Of System.Text.RegularExpressions.Match)().Select(Function(m) m.Value).FirstOrDefault()

- Assign -> Variable2 = System.Text.RegularExpressions.Regex.Matches(Input.ToString, "\d+").Cast(Of System.Text.RegularExpressions.Match)().Select(Function(m) m.Value).LastOrDefault()

Check the below image for better understanding,

Hope it helps!!

1 Like

for advanced Regex Training we would also encourage to explore the named groups capabilities
grafik
grafik

(?<months>\d+(?= month))|(?<week>\d+(?= week))

1 Like

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