fork download
  1. import re
  2.  
  3. regex = r"(?:((\"?)[a-zA-Z]+\2)\s)?(\"[^\"]+\")"
  4. s = ("\"Name\" \"Something to say !\"\n"
  5. "\"Just a descriptive sentence\"\n"
  6. "name \"Something to say !\"\n"
  7. "\"Name\" \"Something to say !\"")
  8.  
  9. matches = re.finditer(regex, s)
  10. for matchNum, match in enumerate(matches, start=1):
  11. print(f"Name: {match.group(1)} Sentence: {match.group(3)}")
Success #stdin #stdout 0.04s 9540KB
stdin
Standard input is empty
stdout
Name: "Name" Sentence: "Something to say !"
Name: None Sentence: "Just a descriptive sentence"
Name: name Sentence: "Something to say !"
Name: "Name" Sentence: "Something to say !"