fork download
  1. import re
  2.  
  3. allowed_words_between = ["and", "with", "a", "very", "beautiful"]
  4. start_signs = [r"\$", "\$\$"]
  5. end_signs = ["Ferrari", "BMW", "Lamborghini", "ship"]
  6. teststring = """
  7. I would like to be a $-millionaire with a Ferrari.
  8. I would like to be a $$-millionair with a Lamborghini.
  9. I would like to be a $$-millionair with a rotten Lamborghini.
  10. I would like to be a $$-millionair with a Lamborghini and a Ferrari.
  11. I would like to be a $-millionaire with a very, very beautiful ship!
  12. I would like to be a $-millionaire with a very, very beautiful but a bit dirty ship.
  13. I would like to be a $-millionaire with a dog, a cat, two children and a cowboy hat. That would be great.
  14. """
  15. regexString = "(?:" + "|".join(start_signs) + ")\S*(?:(?:\s+(?:" + "|".join(allowed_words_between) + "),?)*\s+(?:" + "|".join(end_signs) + "))+"
  16.  
  17. for s in re.findall(regexString, teststring):
  18. print(s)
  19.  
Success #stdin #stdout 0.03s 9524KB
stdin
Standard input is empty
stdout
$-millionaire with a Ferrari
$$-millionair with a Lamborghini
$$-millionair with a Lamborghini and a Ferrari
$-millionaire with a very, very beautiful ship