【问题标题】:Python and Regex: Problem with re findall()Python 和正则表达式:re findall() 的问题
【发布时间】:2020-08-05 00:27:12
【问题描述】:

这是一个找到的项目@https://automatetheboringstuff.com/2e/chapter7/ 它会在剪贴板上的文本中搜索电话号码和电子邮件,然后将结果再次复制到剪贴板。

如果我理解正确,当正则表达式包含组时,findall() 函数会返回一个元组列表。每个元组将包含匹配每个正则表达式组的字符串。

现在这是我的问题:据我所知,phoneRegex 上的正则表达式仅包含 6 个组(在代码上编号)(所以我希望元组的长度为 6)

但是当我打印元组时,我得到长度为 9 的元组

('800-420-7240', '800', '-', '420', '-', '7240', '', '', '')
('415-863-9900', '415', '-', '863', '-', '9900', '', '', '')
('415-863-9950', '415', '-', '863', '-', '9950', '', '', '')

我错过了什么?

#! python3
# phoneAndEmail.py - Finds phone numbers and email addresses on the clipboard.

import pyperclip, re

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

# Create email regex.
emailRegex = re.compile(r'''(
   [a-zA-Z0-9._%+-]+      # username
   @                      # @ symbol
   [a-zA-Z0-9.-]+         # domain name
    (\.[a-zA-Z]{2,4})     # dot-something
    )''', re.VERBOSE)

text = str(pyperclip.paste())

matches = []
for groups in phoneRegex.findall(text):
    print(groups)
    phoneNum = '-'.join([groups[1], groups[3], groups[5]])
    if groups[8] != '':
        phoneNum += ' x' + groups[8]
    matches.append(phoneNum)
for groups in emailRegex.findall(text):
    matches.append(groups[0])

    # Copy results to the clipboard.
if len(matches) > 0:
    pyperclip.copy('\n'.join(matches))
    print('Copied to clipboard:')
    print('\n'.join(matches))
else:
    print('No phone numbers or email addresses found.')

【问题讨论】:

    标签: python-3.x findall python-re


    【解决方案1】:
    括号中的

    Anything 将成为一个捕获组(并在 re.findall 元组的长度上加一),除非您另外指定。要将子组变为非捕获组,请在括号内添加 ?:

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

    您可以看到扩展部分添加了两个额外的捕获组。使用此更新版本,您的元组中将有 7 个项目。有 7 个而不是 6 个,因为整个字符串也是匹配的。

    正则表达式也可以更好。这更简洁,将匹配更多带有re.IGNORECASE 标志的案例:

    phoneRegex = re.compile(r'''(
        (\(?\d{3}\)?)                
        ([\s.-])?                        
        (\d{3})                           
        ([\s.-])                         
        (\d{4})                              
        \s*  # don't need to capture whitespace
        ((?:ext\.?|x)\s*(?:\d{1,5}))?
        )''', re.VERBOSE | re.IGNORECASE)
    

    【讨论】:

      猜你喜欢
      • 2022-11-29
      • 2011-07-18
      • 1970-01-01
      • 2015-08-13
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 2013-07-10
      相关资源
      最近更新 更多