Home > Blockchain >  Regex to match word after keyword, including hypen after keyword
Regex to match word after keyword, including hypen after keyword

Time:02-07

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.

CodePudding user response:

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 )
  •  Tags:  
  • Related