Question

I am trying to match the first word after a specific string using regex and have been struggling to make all instances work properly. Let's say I have 2 strings:

Site Code: foobar

Site Code: hello-world

Now, using (?<=\bSite Code:\s)(\w+) returns foobar which is correct, but only returns hello when I need hello-world instead.

So, I changed my expression to (?<=\bSite Code:\s)(\w+)(-\w+) in order to pickup the hyphenated words, but now it is ignoring the non-hyphenated words.

Is there a way to get both foobar and hello-world from the same expression?

Was it helpful?

Solution 2

Try this regex:

(?<=\bSite Code:\s)([-\w]+)

#\w only include the range [A-Za-z0-9_] in ascii mode.

OTHER TIPS

Try this (?<=\bSite Code:\s)(\S+)

Without more detail on the requirements it's hard to give an exact answer. The above regex should match all non-whitespace characters after the specific string "Site Code: ".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top