Please help in getting the second word (dynamic word which varies in each run) in a line using regex match

Hi Team,

Please help in getting the second word (dynamic word which varies in each run) in a line using regex match.

eg: 00100100000 W123456789 254721 AQQW S

I need second word: W123456789

I used this formula [0-9A-Za-z]{3}\d{7} and getting the first word :00100100000

please help.

Thanks

Hi @Nancy29

You can use the below one,

[A-Z]\d+

image

- Assign -> Input = "00100100000 W123456789 254721 AQQW S"
- Assign -> Output = System.Text.RegularExpressions.Regex.Match(Input.toString,"[A-Z]\d+").Value

Check the below workflow,

Hope it helps!!

1 Like

Hi @Nancy29

Try this is Assign Activity

System.Text.RegularExpressions.Regex.Match("00100100000 W123456789 254721 AQQW S","(?<=\s)\S+").Value

Hope it will helps you :slight_smile:
Cheers!!

hi @mkankatala

Thanks for your quick help. Will try it now.
Also can you please give the formula for getting the fourth word (AQQW) as well

Thanks

Hi @Nawazish_Ahmad

Sure. Thanks for your quick help. Will try this as well.

1 Like

Okay @Nancy29

You can use the below one,

[A-Z]{4}
image

Hope it helps!!

1 Like

Hey @Nancy29
you can use:
^\S+\s+(\S+)
image

secondWord = System.Text.RegularExpressions.Regex.Match(yourInputString, "^\S+\s+(\S+)").Groups(1).Value

Awesome. Thanks a lot.

1 Like

It’s my pleasure… @Nancy29

I hope you find the solution for your query, If yes Make my post mark as solution to close the loop.

Happy Automation!!

1 Like

Thanks a lot. Will try it.

System.Text.RegularExpressions.Regex.Match("00100100000 W123456789 254721 AQQW S","(?:\S+\s+){3}(\S+)").Groups(1).ToString

1 Like

Instead of using match, use matches on any word character \S+
The result is a collection, of which you want the 2nd item. This is much more flexible than the above solutions if you ever need to switch to the 3rd, 4th etc. word.

reference: https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.matches?view=net-8.0

image

System.Text.RegularExpressions.Regex.Matches("Koe Schaap Konijn", "\S+")(1).ToString

(…and for the non-Dutch speaking people here, the words are Cow, Sheep and Rabbit :slight_smile: )

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