python獲取字符串中的email
更新時間:2022年03月31日 11:48:25 作者:aigo-2021
這篇文章主要介紹了python獲取字符串中的email,通過調用re庫,通過使用compile、findall獲取字符串中的email,下文詳細實現(xiàn)過程需要的小伙伴可以參考一下
調用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)
修改:
通過調用函數(shù),并動態(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)
運行結果:
補充:
從指定的字符串中提取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;? ?}
到此這篇關于python獲取字符串中的email的文章就介紹到這了,更多相關獲取字符串中的email內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Django框架中ORM系統(tǒng)實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)增刪改查
這篇文章主要介紹了使用Django的ORM實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)增刪改查方法,文中附含詳細示例代碼以及過程詳解,有需要的朋友可以借鑒參考下2021-09-09詳解python3實現(xiàn)的web端json通信協(xié)議
本篇文章主要介紹了python3實現(xiàn)的web端json通信協(xié)議,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-12-12Python3實現(xiàn)Web網(wǎng)頁圖片下載
這篇文章主要介紹了Python3通過request.urlopen實現(xiàn)Web網(wǎng)頁圖片下載,感興趣的小伙伴們可以參考一下2016-01-01python3實現(xiàn)用turtle模塊畫一棵隨機櫻花樹
今天小編就為大家分享一篇python3實現(xiàn)用turtle模塊畫一棵隨機櫻花樹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11