0

I want to find telephone numbers in string. I use the following code

import re
a = '88005553535'
test = re.compile('(\+?[0-9]{1}( |-)?([0-9]{3}|\\([0-9]{3}\\))( |-)?[0-9]{3}( |-)?[0-9]{2}( |-)?[0-9]{2})+')
print(test.findall(a))

but I have [('88005553535', '', '800', '', '', '')] in result. I realize it happens due to existence of several groups in regex, but I don't understand clearly why do they appear. How should I avoid it?

Nerkan
  • 11
  • 1
  • use `[ -]` instead of `( |-)` – Eraklon Mar 23 '20 at 15:44
  • `( |-)` is a *capturing group*. *`re.findall` returns captured texts if the regex pattern contains capturing groups in it*... *convert all capturing groups into non-capturing (that is, replace `(` with `(?:`) unless there are backreferences that refer to the group values in the pattern...* – Wiktor Stribiżew Mar 23 '20 at 15:47

0 Answers0