0

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!

3
  • 3
    Do you mean like this? \s-\s.*(?<!Dr\. Peper):\s regex101.com/r/llXSX4/1 or \s-(?!\s*Dr\. Peper)\s.*:\s Commented Dec 30, 2019 at 12:37
  • See if this demo helps. Commented Dec 30, 2019 at 12:53
  • Please format your question to be easy to read and understand. Use the {} button in the editor toolbar to mark the code or the data to be processed. Commented Dec 30, 2019 at 12:56

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.