網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
?調(diào)用re庫(kù),通過(guò)使用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)
修改:
通過(guò)調(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;? ?}
原文鏈接:https://blog.csdn.net/aigo_2021/article/details/123859764
相關(guān)推薦
- 2022-10-30 詳解Golang如何實(shí)現(xiàn)一個(gè)環(huán)形緩沖器_Golang
- 2022-07-08 python基礎(chǔ)知識(shí)之索引與切片詳解_python
- 2023-06-17 C語(yǔ)言string庫(kù)strcpy、strcmp、strcat函數(shù)的使用_C 語(yǔ)言
- 2021-12-14 RHCE橋接,免密登錄和修改端口號(hào)介紹_Linux
- 2022-03-29 python自動(dòng)化之re模塊詳解_python
- 2022-06-11 python實(shí)現(xiàn)自動(dòng)整理文件_python
- 2023-12-14 【datetime模塊】將時(shí)間加一秒或者減一秒
- 2022-09-02 Oracle11g調(diào)整SGA方法詳解_oracle
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支