Regex that finds the first word in a string

Hey guys, I’m trying to use the “Matches” activity to find the first word in a string.
This first word can be like “alex”, “1234”, “bg1221” or “1234bg”.
And the full string can look like this:
“`bg134 text text” - from here to extract “bg134”
“bg134 text text” - from here to extract “bg134”
“1234 324 text text” - from here to extract “1234”

The beginning of the string may contain “`” so I need to take that into account.

Can you help me with the regex please ?
Thank!

Test with this:

^\w+

Sorry, this is not working.
The beginning of the string can contain “`” , so it won’t match anything in that case.

Hi @danielP,

i hope it solves the problem.

extractFirstWord.xaml (6.5 KB)

Regards,
MY

1 Like

You can split the strings using " " as separator and retrieving the first item of the resulting array.

Considering you have a variable named ‘text’ you could use this:

text.split(" "c)(0)

you can either retrieve this directly or assign it to a new string variable

1 Like

I had to use the “Matches” activity because it was already implemented in the project.
What I did:

  1. Remove “`” from the string with variableName.Replace(“'”,“”)
  2. Used the “Matches” activity with this regex: “^([a-zA-Z0-9]+)”

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