【问题标题】:Python regex to remove capture email between dashes or ignore emails ending with .jpg etcPython正则表达式删除破折号之间的捕获电子邮件或忽略以.jpg等结尾的电子邮件
【发布时间】:2016-03-07 22:12:22
【问题描述】:

我正在尝试弄清楚如何改进正则表达式以仅获取emails 不以".jpg" 结尾,并从电子邮件的左右部分删除--(如果找到)。示例参数为source,它是一个字符串。

<html>
   <body>
   <p>aaa@example.jpg</p>
   <p>--bbb@example.com--</p>
   <p>ccc@example.com--</p>
   <p>--ddd@example.com</p>

</body>
</html>

结果应包含:bbb@example.com、ccc@example.com、ddd@example.com 所以基本上,我希望看到无论如何改进这个功能,这样正则表达式就可以在没有的情况下生成电子邮件——如果可能的话,改进if not email[0].endswith('.png'),以防我想添加更多,这看起来很紧迫。

def extract_emails(source):

    regex = re.compile(r'([\w\-\.]{1,100}@(\w[\w\-]+\.)+[\w\-]+)')
    emails = list(set(regex.findall(source.decode("utf8"))))
    all_emails = []
    for email in emails:
        if not email[0].endswith('.png') and not email[0].endswith('.jpg') \
                and not email[0].endswith('.gif') and not email[0].endswith('.rar')\
                and not email[0].endswith('.zip') and not email[0].endswith('.swf'):
            all_emails.append(email[0].lower())

    return list(set(all_emails))

【问题讨论】:

  • @Epodax 错误地选择了所有建议的标签。
  • 不要使用正则表达式,使用 html 解析器

标签: python regex email


【解决方案1】:

我认为顶级域名很少,所以你可以使用alternation

s="""<html>
   <body>
   <p>aaa@example.jpg</p>
   <p>--bbb@example.com--</p>
   <p>ccc@example.com--</p>
   <p>--ddd@example.com</p>

</body>
</html>"""
print re.findall(r"-*([\w\.]{1,100}@\w[\w\-]+\.+com|biz|us|bd)-*",s)

['bbb@example.com', 'ccc@example.com', 'ddd@example.com']

DEMO

或试试\w+@\w+\.(?!jpg|png)\w+\.*\w*

s="""<html>
   <body>
   <p>aaa@example.jpg</p>
   <p>--bbb@example.com--</p>
   <p>ccc@example.com--</p>
   <p>--ddd@example.com</p>

</body>
</html>"""
print re.findall(r"\w+@\w+\.(?!jpg|png)\w+\.*\w*",s)

很难为电子邮件验证设置常量正则表达式- 电子邮件验证的详细信息请访问Using a regular expression to validate an email address,它有 69 个答案。

【讨论】:

  • 不尝试我认为它不会通过这个:--ddd@example.com
【解决方案2】:

最好的方法是使用像BeautifulSoup这样的html解析器

In [37]: from bs4 import BeautifulSoup

In [38]: soup = BeautifulSoup('''<html>
   ....:    <body>
   ....:    <p>aaa@example.jpg</p>
   ....:    <p>--bbb@example.com--</p>
   ....:    <p>ccc@example.com--</p>
   ....:    <p>--ddd@example.com</p>
   ....:
   ....: </body>
   ....: </html>''', 'lxml')

In [39]: [email.strip('-') for email in soup.stripped_strings if not email.endswith('.jpg')]
Out[39]: ['bbb@example.com', 'ccc@example.com', 'ddd@example.com']

【讨论】:

    【解决方案3】:
    x="""<html>
       <body>
       <p>aaa@example.jpg</p>
       <p>--bbb@example.com--</p>
       <p>ccc@example.com--</p>
       <p>--ddd@example.com</p>
    
    </body>
    </html>"""
    print re.findall(r"-*([\w\-\.]{1,100}@(?:\w[\w\-]+\.)+(?!jpg)[\w]+)-*",x)
    

    输出:['bbb@example.com', 'ccc@example.com', 'ddd@example.com']

    【讨论】:

    • 对于这个

      222@example.jpg.com

      显然没有完美的电子邮件正则表达式会失败。但是@Uchicha 的正则表达式解决了这个问题。
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 2017-11-08
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多