I am trying to use RegEx in Python to split a string that starts with anything and may or may not end with a year in parentheses into two groups, where the first groups should contain everything but the year, and the second should contain only the year, or nothing if there is no year.
This is what I have so far:
string1 = 'First string'
string2 = 'Second string (2013)'
p = re.compile('(.*)\s*(?:\((\d{4,4})\))?')
print(p.match(string1).groups())
print(p.match(string2).groups())
which code returns this:
('First string', None)
('Second string (2013)', None)
But I'm trying to get this:
('First string', None)
('Second string', '2013')
I realize that the first part in my RegEx is greedy, but I can't find a way to make it not greedy without matching nothing. Also, the first part of my string can contain more or less anything (including parentheses and numbers).
I realize there are ways I can work around this, but since I'm trying to learn RegEx I'd prefer a RegEx solution.