I have a list of strings, each with the following pattern (a set of words followed by parentheses enclosing comma separated words):
"vw xy zz (X, Y, Z)"
My desired output is:
["vw xy zz", "X", "Y", "Z"]
I know how to extract the text before the parentheses:
import re
pattern = r"(^[^\(]+)"
text = "vw xy zz (X, Y, Z)"
re.findall(pattern, text)
# ['vw xy zz ']
I also know how to extract the text between the parentheses:
pattern = r"\(.*\)"
text = "vw xy zz (X, Y, Z)"
re.findall(pattern, text)
# ['(X, Y, Z)']
But I'm wondering if there is a way to combine the patterns to get the desired output all at once.
re.findall(r'[^(),\s](?:[^(),]*[^(),\s])?', s)- all at once with no need to trim the items. Allows any chars but parentheses and commas. Demo