Regex to match word after keyword, including hypen after keyword

Viewed 26

I am trying to write a regex to produce the word after a keyword in a sentence. I have this regex that matches the word after "hello ".

(?<=hello )(\w+)

However, what I am having trouble with is that if, say, hello is hyphenated like hello-bye, then I want to save the hyphen and the word ("-bye"). Any help would be greatly appreciated.

1 Answers

The space then has to optional. But then it can't be used in the negative lookbehind.

If it's fine as long the space isn't part of the capture group.

(?<=hello\b)[ ]*(-?\w+)
Related