samuelbrody1249
samuelbrody1249

Reputation: 4767

Possible to exclude something within a capture group?

Suppose I have a telephone number:

310-5984

I want to capture all numbers in the phone number, that is, 3105984. I can do this with two capturing groups:

>>> re.match(r'(?P<num_1>\d+)-(?P<num_2>\d+)', '310-5984').groupdict()
{'num_1': '310', 'num_2': '5984'}

Is it possible to get the full number with one capturing group? I tried doing something like:

>>> re.match(r'(?P<num>(?P<num_1>\d+)(?:-)(?P<num_2>\d+))', '310-5984').groupdict()
{'num': '310-5984', 'num_1': '310', 'num_2': '5984'}

But it was also consuming the separator. How can I 'consume' the separator but not capture it in a group? Or is that impossible with regex?

Upvotes: 2

Views: 46

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

You could try using re.sub with a callback function, e.g.

def callback(m):
    return m.group(1) + m.group(2)

phone = "310-5984"
number = re.sub('\\b(\d+)-(\d+)\\b', callback, phone)
print(number)

This prints:

3105984

The callback function lets you do an additional step involving all the capture groups obtained from the call to re.sub. In this case, we are just combining them.

Upvotes: 2

Related Questions