Agargara
Agargara

Reputation: 932

Python regex: How to substitute and return matches in one pass?

I want to replace all matches in a string, and also get an array of the matches.

Here's how to do it with two functions:

str = "foo123bar456"
nums = re.findall(r'\d+', str)
str = re.sub(r'\d+', '', str)    

But this goes through the string twice unnecessarily. How can I do this in one pass?

Upvotes: 4

Views: 806

Answers (2)

anubhava
anubhava

Reputation: 785581

Using a lambda function in re.sub:

>>> str = "foo123bar456"
>>> arr=[]
>>> print re.sub(r'(\d+)', lambda m: arr.append(m.group(1)), str)
foobar
>>> print arr
['123', '456']

Upvotes: 4

Paco H.
Paco H.

Reputation: 2054

In re.sub the argument repl can be a function that returns a string. We can use this to add the matches to a list:

import re


def substitute(string):
    def _sub(match):
        matches.append(match)
        return ''

    matches = []
    new_string = re.sub(r'\d+', _sub, string)
    return new_string, matches


print(substitute('foo123bar456'))
> ('foobar', [<_sre.SRE_Match object; span=(3, 6), match='123'>, <_sre.SRE_Match object; span=(9, 12), match='456'>])

Upvotes: 2

Related Questions