You might use a capture group for "
with a backreference to either match or not match the accompanying double quote.
Then you can make the whole first part including the whitespace char optional, and match the second part between double quotes.
Note that [a-zA-z]
matches more than [a-zA-Z]
and the ?
inside the character class matches the question mark literally.
The matches are in group 1 and group 3.
(?:(("?)[a-zA-Z]+\2)\s)?("[^"]+")
(?:
Non capture group
(
Capture group 1
("?)
Capture an optional "
in group 2
[a-zA-Z]+
Match a+ times a char a-zA-Z a
\2
A backreference to group 2 to match exactly what is matched in that group
)\s
Close group 1 and match a whitespace char
)?
Close the non capture group and make it optional
("[^"]+")
Capture group 3, match from "
till "
See a regex demo | Python demo
Example using re.finditer looping the matches:
import re
regex = r"(?:((\"?)[a-zA-Z]+\2)\s)?(\"[^\"]+\")"
s = ("\"Name\" \"Something to say !\"\n"
"\"Just a descriptive sentence\"\n"
"name \"Something to say !\"\n"
"\"Name\" \"Something to say !\"")
matches = re.finditer(regex, s)
for matchNum, match in enumerate(matches, start=1):
print(f"Name: {match.group(1)} Sentence: {match.group(3)}")
Output
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 !"