網(wǎng)站首頁 編程語言 正文
?調(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ù),并動態(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)
運行結(jié)果:
補充:
從指定的字符串中提取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
相關(guān)推薦
- 2022-07-31 python中opencv?K均值聚類的實現(xiàn)示例_python
- 2022-09-19 用正則表達(dá)式匹配字符串中漢字及中文標(biāo)點符號_正則表達(dá)式
- 2022-07-21 UIStackView嵌套的Label多行文字動態(tài)高度
- 2022-05-25 python?序列去重并保持原始順序操作_python
- 2022-04-15 c語言?指針零基礎(chǔ)講解_C 語言
- 2022-09-25 MyBatis實現(xiàn)多表查詢(一對一、一對多)的方式
- 2023-12-26 錯誤代碼: 1062Duplicate entry ‘304‘ for key ‘tb_clue_t
- 2022-01-18 django中ajax發(fā)送post請求報403錯誤csrf禁止,解決只需三步
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支