fork download
  1. import re
  2.  
  3. pattern = r"\bJane(?:'?s|\(s\))? car\b|\b(car)\b"
  4.  
  5. s = ("for example\n"
  6. " the car is red - Match\n"
  7. " here is Jane car is red -> None\n"
  8. " here is Janes car is red -> None\n"
  9. " here is Jane's car is red -> None\n\n"
  10. "2. I also want to find the cases Jane is in the phrase\n"
  11. " the car is red - None\n"
  12. " here is Jane car is red - Match\n"
  13. " here is Janes car is red - Match\n"
  14. " here is Jane's car is red - Match\n\n"
  15. "3. and where car is not preceding by Jane(s)\n"
  16. " here Jane(s) car is red - None\n\n"
  17. "4. and of course the opposite\n"
  18. " here is Jane(s) car is red - Match")
  19. result = [m for m in re.findall(pattern, s) if m]
  20. print(result)
Success #stdin #stdout 0.03s 9388KB
stdin
Standard input is empty
stdout
['car', 'car', 'car']