Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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 3.4.0_Python_Python 3.x_Regex - Fatal编程技术网

正则表达式|空字符串匹配| Python 3.4.0

正则表达式|空字符串匹配| Python 3.4.0,python,python-3.x,regex,Python,Python 3.x,Regex,我的代码: import re #Phone Number regex phoneRegex = re.compile(r'''( (\d{3}|\(\d{3}\))? (\s|-|\.)? # separator (\d{3}) # first 3 digits (\s|-|\.) # separator (\d{4})

我的代码:

import re

#Phone Number regex
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))?
(\s|-|\.)?                       # separator
(\d{3})                          # first 3 digits
(\s|-|\.)                        # separator
(\d{4})                          # last 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))?   # extension
)''', re.VERBOSE)

phoneRegex.findall('Phone: 800.420.7240 or +1 415.863.9900 (9 a.m. to 5 p.m., M-F, PST)')
[('800.420.7240', '800', '.', '420', '.', '7240', '', '', ''), ('415.863.9900', '415', '.', '863', '.', '9900', '', '', '')]
输出:

import re

#Phone Number regex
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))?
(\s|-|\.)?                       # separator
(\d{3})                          # first 3 digits
(\s|-|\.)                        # separator
(\d{4})                          # last 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))?   # extension
)''', re.VERBOSE)

phoneRegex.findall('Phone: 800.420.7240 or +1 415.863.9900 (9 a.m. to 5 p.m., M-F, PST)')
[('800.420.7240', '800', '.', '420', '.', '7240', '', '', ''), ('415.863.9900', '415', '.', '863', '.', '9900', '', '', '')]
问题:

import re

#Phone Number regex
phoneRegex = re.compile(r'''(
(\d{3}|\(\d{3}\))?
(\s|-|\.)?                       # separator
(\d{3})                          # first 3 digits
(\s|-|\.)                        # separator
(\d{4})                          # last 4 digits
(\s*(ext|x|ext.)\s*(\d{2,5}))?   # extension
)''', re.VERBOSE)

phoneRegex.findall('Phone: 800.420.7240 or +1 415.863.9900 (9 a.m. to 5 p.m., M-F, PST)')
[('800.420.7240', '800', '.', '420', '.', '7240', '', '', ''), ('415.863.9900', '415', '.', '863', '.', '9900', '', '', '')]
  • 为什么匹配中包含空字符串
  • 空字符串从字符串的哪个位置匹配
  • 空字符串的匹配条件是什么
  • p.S. 当我在
    上使用相同的正则表达式时,匹配中不包括空字符串
    另外,我几天前才开始学习regex,如果我的问题不够好,我很抱歉。

    操作符
    意味着它将返回0或1个匹配项。在本例中,您使用
    将一些捕获组设置为可选,python将为您创建的三个可选捕获组中的每一个返回零长度匹配

    如果删除前两个
    ,将消除一些零长度匹配。要处理最后一个问题,您需要更改扩展模式。它占两个,同样是因为您使用了一个零或一个运算符(
    *

    如果您不关心内部捕获组,只需要完整匹配,您可以使用以下方法过滤掉零长度匹配

    >>> [match.group(0) for match in phoneRegex.finditer('Phone: 800.420.7240 or +1 415.863.9900 (9 a.m. to 5 p.m., M-F, PST)')]
    ['800.420.7240', '415.863.9900']
    
    您可以使分机捕获组匹配,条件是前面的电话号码匹配。此外,我认为您可能需要在第三个备选方案
    分机中退出
    。正如所写的,它匹配任何字符,但我认为你的意思是
    ext\.

    供参考:


    为什么匹配中包含空字符串? 因为您在正则表达式中使用了各种组。引擎将捕获您放入组中的匹配部分

    空字符串从字符串的哪个位置匹配? 从这个正则表达式:
    (\s*(ext | x | ext.)\s*(\d{2,5}))?
    它有三个组(可以计算左括号)。引擎找不到与扩展名匹配的内容,尝试捕获信息的3个组返回空字符串

    匹配空字符串的条件是什么? 如果以引擎捕获匹配字符串中的空子字符串的方式对正则表达式进行分组,它将返回空字符串:-)

    我认为您正在遵循“用python自动化无聊的东西”中的练习。在178页的VERBOSE模式下的正则表达式中,尝试查找左括号。右括号在哪里?组的数量与左括号的数量相等。整个正则表达式的组号为零

    如果要提取匹配字符串的某些部分,组非常有用。如果您只想提取完整的电话号码,请将通话组留在一旁

    你可以试试这个:

    phoneRegex = re.compile(r'\d{3}[\.|-|\/]\d{3}[\.|-|\/]\d{4}')
    
    这就是你想要实现的吗

    如果您想在详细模式下使用正则表达式,还可以使用非捕获组。这仅捕获完整匹配:

    phoneRegex = re.compile(r'''(
    (?:\d{3}|\(?:\d{3}\))?
    (?:\s|-|\.)?                       # separator
    (?:\d{3})                          # first 3 digits
    (?:\s|-|\.)                        # separator
    (?:\d{4})                          # last 4 digits
    (?:\s*(?:ext|x|ext.)\s*(?:\d{2,5}))?   # extension
    )''', re.VERBOSE)
    

    谢谢你的回答!即使删除了前两个(?)运算符,我仍然会得到3个空字符串匹配项。在删除(*)运算符之后,我尝试运行代码,但仍然得到3个空字符串匹配项。但是,当我删除最后一个(?)操作符(与“extension”一起使用)时,我没有得到任何匹配。