Home > Software engineering >  Regex: how to tell a string from substring?
Regex: how to tell a string from substring?

Time:10-31

I have a string: "John and John Doe: Hello."

How to use Regex to make sure that this string contains "John" but not "John Doe". Or vise versa - if it contains "John Doe" but not "John". "John" and "John Doe" are two different persons. I'm at my wits end.

CodePudding user response:

I this case I would not use a single match but two. The use an exclusive OR.

CodePudding user response:

Negative look-ahead:

John(?! Doe)
  • Related