python获取字符串中的email
作者:aigo-2021 时间:2021-04-23 21:23:29
调用re库,通过使用compile、findall获取字符串中的email
import re
email=re.compile(r'[a-z0-9\-\.]+@[0-9a-z\-\.]+')
emailtest=r'adfasldfjdsl fan02@163.com'
emailset=set()
for em in email.findall(emailtest):
emailset.add(em)
for em1 in sorted(emailset):
print(em1)
修改:
通过调用函数,并动态地为字符串赋值
import re
def emailre(teststr):
email=re.compile(r'[a-z0-9\-\.]+@[0-9a-z\-\.]+')
emailset=set() #列表
for em in email.findall(teststr):
emailset.add(em)
for eml in sorted(emailset):
print(eml)
emailtest='sdfdsgf asd03@162.com'
emailre(emailtest)
#或
strtest=r'sdgfsg abc@163.com'
emailre(strtest)
运行结果:
补充:
从指定的字符串中提取Email:
/**
* 从指定的字符串中提取Email
* content 指定的字符串
*/
public static String parse(String content) {
String email = null;
if (content==null || content.length()<1) {
return email;
}
//找出含有@
int beginPos;
int i;
String token = "@";
String preHalf="";
String sufHalf = "";
beginPos = content.indexOf(token);
if (beginPos>-1) {
//前项扫描
String s = null;
i= beginPos;
while(i>0) {
s = content.substring(i-1,i);
if (isLetter(s))
preHalf = s+preHalf;
else
break;
i--;
}
//后项扫描
i= beginPos+1;
while( i<content.length()) {
s = content.substring(i,i+1);
if (isLetter(s))
sufHalf = sufHalf +s;
else
break;
i++;
}
//判断合法性
email = preHalf + "@" + sufHalf;
if (isEmail(email)) {
return email;
}
}
return null;
}
/**
* 判断是不是合法Email
* email Email地址
*/
public static boolean isEmail(String email) {
try {
if (email==null || email.length()<1 || email.length()>256) {
return false;
}
String check = "^([0-9a-zA-Z]+[_.0-9a-zA-Z-]+)@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2,3})$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
boolean isMatched = matcher.matches();
if(isMatched) {
return true;
} else {
return false;
}
} catch (RuntimeException e) {
return false;
}
}
/**
* 判断是不是合法字符
* c 要判断的字符
*/
public static boolean isLetter(String c) {
boolean result = false;
if (c==null || c.length()<0) {
return false;
}
//a-z
if (c.compareToIgnoreCase("a")>=0 && c.compareToIgnoreCase("z")<=0) {
return true;
}
//0-9
if (c.compareToIgnoreCase("0")>=0 && c.compareToIgnoreCase("9")<=0) {
return true;
}
//. - _
if (c.equals(".") || c.equals("-") || c.equals("_") ) {
return true;
}
return result;
}
来源:https://blog.csdn.net/aigo_2021/article/details/123859764
标签:python,获取,字符串,email
0
投稿
猜你喜欢
获取CSDN文章内容并转换为markdown文本的python
2021-04-05 08:32:15
判断触发器正在处理的是插入,删除还是更新触发
2012-01-29 18:30:34
MySQL字符集乱码及解决方案分享
2024-01-21 12:00:43
一个将半角"转换为中文"的asp函数
2007-09-19 11:47:00
MySQL数据库的索引原理与慢SQL优化的5大原则
2024-01-18 14:46:01
如何通过命令行进入python
2022-10-28 22:12:56
PDO::inTransaction讲解
2023-06-06 08:32:27
微信小程序报错: thirdScriptError的错误问题
2024-04-19 09:47:43
pytorch::Dataloader中的迭代器和生成器应用详解
2021-08-31 03:09:30
Django 项目重命名的实现步骤解析
2021-01-13 14:05:49
mysql 5.7.17 winx64解压版安装配置方法图文教程
2024-01-22 07:47:16
Django 中使用流响应处理视频的方法
2021-01-12 20:55:13
VSCode 最全实用插件小结
2022-12-11 17:03:47
pytorch中关于distributedsampler函数的使用
2023-01-18 01:10:01
Python中常见的数据类型小结
2022-03-25 07:55:17
Python标准库使用OrderedDict类的实例讲解
2022-07-17 22:27:31
Django QuerySet查询集原理及代码实例
2023-04-06 01:30:18
golang常用库之操作数据库的orm框架-gorm基本使用详解
2024-01-28 21:22:19
openfiledialog读取txt写入数据库示例
2024-01-16 02:03:35
Bootbox将后台JSON数据填充Form表单的实例代码
2023-08-22 22:01:21