Let's say I have a string like this...
myStr = 'START1(stuff); II(morestuff); 8(lessstuff)'
...and I want to extract the string immediately before the parentheses, as well as the string within the parentheses: 1, stuff, II, morestuff, 8, lessstuff. I can achieve this using split(';'), etc., but I want to see if I can do it in one fell swoop with re.search(). I have tried...
test = re.search( r'START(?:([I0-9]+)\(([^)]+?)\)(?:; )?)*', myStr ).groups()
...or in a more readable format...
test = re.search( r'''
START # This part begins each string
(?: # non-capturing group
([I0-9]+) # capture label before parentheses
\(
([^)]+?) # any characters between the parentheses
\)
(?:; )? # semicolon + space delimiter
)*
''', myStr, re.VERBOSE ).groups()
...but I only get the last hit: ('8', 'lessstuff'). Is there a way to backreference multiple hits of the same part of the expression?
START1or just1?re.VERBOSEflag first: docs.python.org/2/library/re.html#re.VERBOSE ;-)1. I could have leftSTARToff for the purposes of this question.