python使用正则验证电子邮件,使用正则表达式python查找电子邮件

I want to find valid email addresses in a text file, and this is my code:

email = re.findall(r'[a-zA-Z\.-]+@[\w\.-]+',line)

But my code obviously does not contain email addresses where there are numbers before @ sign. And my code could not handle email addresses that do not have valid ending. So could anyone help me with these two problems? Thank you!

An example of my problem would be:

my code can find this email: xyz@gmail.com

but it cannot find this one: xyz123@gmail.com

And it cannot filter this email out either: xyz@gmail

解决方案

From the python re docs, \w matches any alphanumeric character and underscores, equivalent to the set [a-zA-Z0-9_]. So [\w\.-] will appropriately match numbers as well as characters.

email = re.findall(r'[\w\.-]+@[\w\.-]+(\.[\w]+)+',line)

This post discusses matching email addresses much more extensively, and there are a couple more pitfalls you run into matching email addresses that your code fails to catch. For example, email addresses cannot be made up entirely of punctuation (...@....). Additionally, there is often a maximum length on addresses, depending on the email server. Also, many email servers match non-english characters. So depending on your needs you may need a more comprehensive pattern.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python使用正则表达式的方法是通过re库来实现的。re库是Python中提供的用于处理正则表达式的库。可以使用re库中的不同方法来实现不同的功能。常用的方法包括match()、findall()等。 match()方法用于在字符串的开头匹配正则表达式。它返回一个匹配对象,包含匹配的结果。如果匹配成功,则返回匹配对象;否则返回None。可以通过匹配对象的span()方法获取匹配的起始位置和结束位置,通过group()方法获取匹配的字符串。例如,对于字符串s = 'python123python666python888',使用re.match('python', s)可以匹配到'python',返回的匹配对象的span()方法返回(0, 6),group()方法返回'python'。 findall()方法用于在字符串中找到正则表达式所匹配的所有子串,并返回一个列表。如果没有找到匹配的,则返回空列表。例如,对于字符串s = '1python123python666python888',使用re.findall('python', s)可以匹配到三个'python',返回的列表为['python', 'python', 'python']。 通过使用re库的不同方法,可以灵活地使用正则表达式来进行字符串的匹配和处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python-如何使用正则表达式](https://blog.csdn.net/weixin_47221728/article/details/116357287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python使用正则表达式](https://blog.csdn.net/a15608445683/article/details/124650730)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值