Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式查找'\n';_Python_Regex - Fatal编程技术网

Python 正则表达式查找'\n';

Python 正则表达式查找'\n';,python,regex,Python,Regex,我正在制作一个程序,在文本中对电话号码进行模式匹配 我正在加载此文本: (01111-222222)fdf 01111222222 (01111)222222 01111 222222 01111.222222 输入一个变量,并使用“findall”返回: ('(01111-222222)', '(01111', '-', '222222)') ('\n011112', '', '\n', '011112') ('(01111)222222', '(01111)', '', '222222')

我正在制作一个程序,在文本中对电话号码进行模式匹配

我正在加载此文本:

(01111-222222)fdf
01111222222
(01111)222222
01111 222222
01111.222222
输入一个变量,并使用“findall”返回:

('(01111-222222)', '(01111', '-', '222222)')
('\n011112', '', '\n', '011112')
('(01111)222222', '(01111)', '', '222222')
('01111 222222', '01111', ' ', '222222')
('01111.222222', '01111', '.', '222222')
这是我的表达:

ex = re.compile(r"""(
    (\(?0\d{4}\)?)?       # Area code
    (\s*\-*\.*)?          # seperator
    (\(?\d{6}\)?)        # Local number
     )""", re.VERBOSE)
我不明白为什么“\n”会被抓到


如果“
\\.*
”中的
*
被“
+
”替换,则表达式可以按我的要求工作。或者,如果我只是删除了
*
(并且很高兴发现两组数字之间仅用一个句点分隔),则表达式可以工作。

\s
同时匹配水平和真实的空格符号。如果您有
re.VERBOSE
,则可以将普通空格与转义空格
\
匹配。或者,您可以使用
[^\s\r\n]
\s
中排除
\r
\n
,以匹配水平空白

使用


此外,字符类外部的
-
不需要转义。

\s*
包含
\n
不匹配换行符,除非设置了
re.DOTALL
(或
re.s
)标志。我刚刚意识到我的最后一段事实上不是真的。我根本没有正确地查看我的结果。
ex = re.compile(r"""(
    (\(?0\d{4}\)?)?       # Area code
    ([^\S\r\n]*-*\.*)?   # seperator   ((HERE))
    (\(?\d{6}\)?)        # Local number
     )""", re.VERBOSE)