David
David

Reputation: 3348

Match anything until first occurrence of word with regex

I have the following sentence:

I'm screaming to my friend hello, hello, hello!

I want to match anything between the leading I'm and the first occurrence of the word hello.

I have the following regular expression:

I'm[^hello]*

This is working very well if I had a single character instead of a whole word, in my case hello.

It seams like hello isn't recognized as a whole word but as single characters with an OR operator between them.

The result I get is: I'm scr

The expected result is: I'm screaming to my friend

How can I match anything until the first occurrence of the word hello?

Upvotes: 1

Views: 399

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522636

You may try:

I'm (?:(?!\bhello\b).)*

If your regex tool/language support capture groups, then use them instead:

(I'm .*?)(?:\bhello\b|$)

Upvotes: 1

Related Questions