Skip to content Skip to sidebar Skip to footer

Regular Expression Only Captures The Last Occurence Of Repeated Group

I am trying to capture multiple ' = ' pairs with a Python regular expression from a string like this: some(code) '

Solution 1:

This is just how regex works : you defined one capturing group, so there is only one capturing group. When it first captures something, and then captures an other thing, the first captured item is replaced.That's why you only get the last captured one. There is no solution for that that I am aware of...

Solution 2:

Unfortunately this is not possible with python's re module. But regex provides captures and capturesdict functions for that:

>>> m = regex.match(r"(?:(?P<word>\w+) (?P<digits>\d+)\n)+", "one 1\ntwo 2\nthree 3\n")
>>> m.groupdict()
{'word': 'three', 'digits': '3'}
>>> m.captures("word")
['one', 'two', 'three']
>>> m.captures("digits")
['1', '2', '3']
>>> m.capturesdict()
{'word': ['one', 'two', 'three'], 'digits': ['1', '2', '3']}

Solution 3:

From the documentation search will return only one occurrence. The findAll method returns all occurrences in the list. That is what you need to use, like in your second example.

Post a Comment for "Regular Expression Only Captures The Last Occurence Of Repeated Group"