Python 正则表达式 : find zip from html content

标签 python regex

我有一个电子邮件模板,其中包含 html 格式的电子邮件上下文,

现在我想从电子邮件 html 内容中查找邮政编码,

为此,我使用正则表达式来搜索邮政编码, 内容就像 甲酸盐1:

helllo this is the mail  which will converted in the lead 
and here is some addresss  which will not be used..

and the 
zip: 364001
city: New york

甲酸盐2:

<p><b>Name</b></p><br/>
fname
<p><b>Last Name</b></p><br/>
lname
<p><b>PLZ</b></p><br/>
71392
<p><b>mail</b></p><br/>
heliconia72@mail.com

代码看起来像,

regex = r'(?P<zip>Zip:\s*\d\d\d\d\d\d)'
zip_match = re.search(regex, mail_content) # find zip
zip_match.groups()[0]

这只是搜索 fomate 2,我如何编写正则表达式,使其适用于这两个 formate。

最佳答案

如果您确实需要为此使用正则表达式(第二个我可能会使用 BeautifulSoup),您可以使用以下示例:

regex = r'(?:zip:\s*|PLZ</b></p><br/>\n)(\d{5})'
zip_match = re.search(regex1, mail_content)
zip_match.groups()[0]

关于Python 正则表达式 : find zip from html content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18396051/

相关文章:

python - 在字符串中查找特定单词,Python

regex - 如何在 Notepad++ 中提取方括号之间的逗号?

php - 我将如何使用正则表达式来解析这个和弦方案?

python - Python 中的 OpenCV grabcut() 背景颜色和轮廓

python - 为什么我收到 AttributeError : Object has no attribute?

python - 通过部分类名在 BS4 中查找 "a"元素不起作用?

MySQL 字符串匹配多个单词

regex - Google BigQuery 可以做不区分大小写的 REGEXP_Match 吗?

python - 绘制彼此靠近的多条线时出现白点

python - 在 Numpy 中连接空数组