I have a text file consisting of space-separate text values:
a: b c d e f g
h: i j k
l:
m: n
I do not know how many of these values - right of ;
- I'll have a priori.
I want to use Python groups within a regular expression to be able to refer to each capture.
GnuATgtRE = re.compile(br'^\r\n(?P<target>.+): (?P<deps>.*)\r\n# Implicit rule search has', re.MULTILINE)
Currently, <target>
references the item to the left of semi-colon and <deps>
references everything, in one string, to the right.
I do not know a priori how many deps
each target
will have.
The syntax (?P<text>)
is used to create a group which can be used to reference a specific captured sub-regex.
For example, for line 1
match_obj.group('target')
= a
match_obj.group('deps')
= b c d e f g
Line 2:
match_obj.group('target')
= h
match_obj.group('deps')
= i j k
Question
After I execute match = GnuATgtRE.search(string)
, I want to be able to be able to reference each space-separate dep
via match.group('some_text')
.
The problem is that I don't know if there is a way to create an arbitrary number of unnamed groups.
For line 1, I'd like to be able to say match.group('<5>')
and have that return d
.
For line 2, match.group('<5')
should return `` since there's only 5 letters.