pm1359
pm1359

Reputation: 632

What this regular expression is (re.compile) and when we can use the pattern naming in python?

I have found the following regular expression, and I don't understand what does that mean? I need help to understand that. Specially 'p' in middle of everything:

RE_CALLEE = re.compile(r'(?P<callee>\+[0-9]+)')

This is an example of code:

2015-11-01T00:00:17.735616+00:00 x1ee energysrvpol[15690]: INFO consume_processor: user:<<"dbdiayhg">> callee_num:<<"+1288888888">> sid:<<"A1003unjhjhvhgfgvhbghgujhj02">> credits:-0.5000000000000001 result:ok provider:ooioutisrt.ym.ms

Upvotes: 0

Views: 80

Answers (1)

alecxe
alecxe

Reputation: 474003

  • the (?P<callee>) part is a named capturing group
  • the \+[0-9]+ would match a plus character followed by one or more digits

Here is how you can get the group by it's name:

>>> import re
>>> RE_CALLEE = re.compile(r'(?P<callee>\+[0-9]+)')
>>>
>>> RE_CALLEE.search("test +10").group("callee")
'+10'

As for your example, how about this pattern:

>>> RE_CALLEE = re.compile(r'callee_num:<<"(\+\d+)"')
>>> RE_CALLEE.findall(s)
['+1288888888']

Upvotes: 2

Related Questions