Christoph
Christoph

Reputation: 59

REGEX Question: Match regex, but exclude a specific string

With the Regex \s-\s.*:\s I successfully match the following:

22/12/2015, 17:31 - Dr. Peper: My Message 22/12/2015, 17:01 - Frank: MESSAGE MA MA MA MA asdf aysfjsfdl asdfoasdf 22/12/2015, 17:18 - Sepp: MESSAGE ----- XXX 22/12/2015, 17:31 - Dr. Peper: My Message

However, now I want to exclude the string "Dr Peper" from the matching. How can I get the following as a result?

22/12/2015, 17:31 - Dr. Peper: My Message 22/12/2015, 17:01 - Frank: MESSAGE MA MA MA MA asdf aysfjsfdl asdfoasdf 22/12/2015, 17:18 - Sepp: MESSAGE ----- XXX 22/12/2015, 17:31 - Dr. Peper: My Message

"Sepp" and "Frank" are arbitrary strings.

Thank you so much for your answers!

Upvotes: 0

Views: 65

Answers (1)

The fourth bird
The fourth bird

Reputation: 163577

The .* part of the pattern will first match until the end of the string and will then backtrack until the first : that can be followed by a whitespace char.

If you want to match a whitespace char and then until the first occurrence of :, I would suggest using a negated character class [^:\r\n]* instead matching any char except a : or a newline.

To match the names except Dr. Peper, you could use a negative lookahead to assert what follows is not 0+ whitespace chars an Dr. Peper:

\s-(?!\s*Dr\. Peper:)\s[^:\r\n]*:\s

Regex demo

Upvotes: 1

Related Questions