Regular Expression Search '\ n'

I am in the process of creating a program for matching phone numbers in the text.

I am loading this text:

(01111-222222)fdf
01111222222
(01111)222222
01111 222222
01111.222222

To the variable using "findall", it returns this:

('(01111-222222)', '(01111', '-', '222222)')
('\n011112', '', '\n', '011112')
('(01111)222222', '(01111)', '', '222222')
('01111 222222', '01111', ' ', '222222')
('01111.222222', '01111', '.', '222222')

This is my expression:

ex = re.compile(r"""(
    (\(?0\d{4}\)?)?       # Area code
    (\s*\-*\.*)?          # seperator
    (\(?\d{6}\)?)        # Local number
     )""", re.VERBOSE)

I don’t understand why "\ n" gets in.

If *in ` \\.*` is replaced by ' +', the expression works the way I want it. Or, if I just delete *(and gladly find two sets of numbers separated by only one period), the expression works.

+4
source share
1 answer

\s , . re.VERBOSE, \ . \r \n \s [^\S\r\n] .

ex = re.compile(r"""(
    (\(?0\d{4}\)?)?       # Area code
    ([^\S\r\n]*-*\.*)?   # seperator   ((HERE))
    (\(?\d{6}\)?)        # Local number
     )""", re.VERBOSE)

regex

, - .

+4

Source: https://habr.com/ru/post/1657591/


All Articles