python正则表达式中的整词

2024-04-19 15:41:06 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在Python中使用正则表达式查找整词? 我使用Beautiful soup和re library来解析文档。在汤里,我需要找到“E-mail”之后的所有内容。我尽力了

for sublink in link.findAll(text = re.compile("[E-mail:0-9a-zA-Z]")):
         print sublink.encode('utf-8') 

但它不起作用。你知道吗


Tags: textin文档re内容forlibrarylink
1条回答
网友
1楼 · 发布于 2024-04-19 15:41:06

下面是一个通过正则表达式提取单词的工作示例:

import re

text = "First line\n" + \
    "Second line\n" + \
    "Important line! E-mail:mail@domain.de, Phone:991\n" + \
    "Another important line! E-mail:tom@gmail.com, Phone:001\n" + \
    "Another line"
print text

emails = re.findall("E-mail:([\w@.-]+)", text)
print "Found email(s): " + ', '.join(emails)

输出:

Found email(s): mail@domain.de, tom@gmail.com

不知道你要找的是不是这个。你知道吗

编辑:字符0-9a-zA-Z可以写成\w。是的,我添加了.-。如果有更多可能的字符,只需将它们放入[\w@.-]。你知道吗

相关问题 更多 >