fork download
  1. import re
  2.  
  3. strings = [
  4. "the owner has a dog",
  5. "the owner has a black and brown dog",
  6. "John has a dog",
  7. "John has a black and brown dog"
  8. ]
  9.  
  10. pattern = r"^[^o\r\n]*(?:(?!\bowner\b)o[^o\r\n]*)*\bdog\b"
  11.  
  12. for s in strings:
  13. res = re.search(pattern, s)
  14. print(res)
Success #stdin #stdout 0.02s 9504KB
stdin
Standard input is empty
stdout
None
None
<re.Match object; span=(0, 14), match='John has a dog'>
<re.Match object; span=(0, 30), match='John has a black and brown dog'>