python獲取字符串中的email
調(diào)用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)
修改:
通過調(diào)用函數(shù),并動(dòng)態(tài)地為字符串賦值
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)
運(yùn)行結(jié)果:

補(bǔ)充:
從指定的字符串中提取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) {
?//前項(xiàng)掃描
?String s = null;
? i= beginPos;
?while(i>0) {
s = content.substring(i-1,i);
?if (isLetter(s))
? ? preHalf = s+preHalf;
? else
? ? break;
? i--;
? }
?//后項(xiàng)掃描
? 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;?
?}到此這篇關(guān)于python獲取字符串中的email的文章就介紹到這了,更多相關(guān)獲取字符串中的email內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 26進(jìn)制計(jì)算實(shí)現(xiàn)方法
這篇文章主要介紹了Python 26進(jìn)制計(jì)算實(shí)現(xiàn)方法,涉及Python字符串與數(shù)值計(jì)算的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05
Python 如何實(shí)現(xiàn)文件自動(dòng)去重
這篇文章主要介紹了Python 實(shí)現(xiàn)文件自動(dòng)去重操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Pytorch torch.repeat_interleave()用法示例詳解
torch.repeat_interleave() 是 PyTorch 中的一個(gè)函數(shù),用于按指定的方式重復(fù)張量中的元素,這篇文章主要介紹了Pytorch torch.repeat_interleave()用法示例詳解,需要的朋友可以參考下2024-01-01
python爬取免費(fèi)代理并驗(yàn)證代理是否可用
這篇文章主要介紹了python爬取免費(fèi)代理并驗(yàn)證是否可用,通過本文給大家介紹了在什么情況下會(huì)用到代理并分享腳本的完整代碼,需要的朋友可以參考下2022-01-01
Python + Flask 實(shí)現(xiàn)簡單的驗(yàn)證碼系統(tǒng)
這篇文章主要介紹了Python + Flask 制作一個(gè)簡單的驗(yàn)證碼系統(tǒng),本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
使用Python實(shí)現(xiàn)從零開始打造一個(gè)三維繪圖系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)一個(gè)繪圖系統(tǒng),通過指定x,y,z的表達(dá)式,以實(shí)現(xiàn)三維繪圖的目的,感興趣的可以了解下2024-02-02

