1. *表示匹配任意多个字符 \d*表示匹配任意多个数字字符
import re text = "123h1ello world" text1 = "123Hello world456" text2 = "hello world" res = re.match("\d*", text) res1 = re.match("\d*", text1) res2 = re.match("\d*", text2) print(res.group()) print(res1.group()) print(res2.group())
输出结果为
123
123Process finished with exit code 0
示例2:*
需求:匹配出,一个字符串第一个字母为大小字符,后面都是小写字母并且这些小写字母可有可无
import re #注意是前一个字符 ret = re.match("[A-Z][a-z]*","M") print(ret.group()) ret = re.match("[A-Z][a-z]*","AaBcDE") print(ret.group()) ret = re.match("[A-Z][a-z]*","Aabcdef") print(ret.group())
#运行结果
M
Aa
Aabcdef
2. +表示匹配1个或者多个任意字符 \w+表示匹配一个或多个字母,数字或下划线
import re text = "he+llo world" text1 = "Hello world456" text2 = "+hello world" res = re.match("\w+", text) res1 = re.match("\w+", text1) res2 = re.match("\w+", text2) print(res.group()) print(res1.group()) print(res2)
输出结果为
he
Hello
NoneProcess finished with exit code 0
示例2:+
需求:匹配出,变量名是否有效
import re names = ["name1","_name","2_name","__name__"] for i in names: ret = re.match("[a-zA-Z_]+[\w]*",i) if ret: print("变量名 %s 符合要求" % ret.group()) else: print("变量名 %s 非法" % i)
#运行结果
变量名 name1 符合要求
变量名 _name 符合要求
变量名 2_name 非法
变量名 __name__ 符合要求
3. "htmlcode">
import re
text = "he+llo world"
text1 = "Hello world456"
text2 = "+hello world"
res = re.match("\w", text)
res1 = re.match("\w", text1)
res2 = re.match("\w", text2)
print(res.group())
print(res1.group())
print(res2.group())
import re text = "he+llo world" text1 = "Hello world456" text2 = "+hello world" res = re.match("\w", text) res1 = re.match("\w", text1) res2 = re.match("\w", text2) print(res.group()) print(res1.group()) print(res2.group())
输出结果为
h
HProcess finished with exit code 0
示例2:?
需求:匹配出0到99之间的数字
import re ret = re.match("[1-9]","7") print(ret.group()) ret = re.match("[1-9]","33") print(ret.group()) ret = re.match("[1-9]","09") print(ret.group())
#运行结果
7
33
0 # 这个结果并不是想要的,利用$才能解决
4. {m}表示匹配m个字符 \d{11}表示匹配11个数字字符
import re text = "he+llo world" text1 = "Hello world456" text2 = "hello world" res = re.match("\w{2}", text) res1 = re.match("\w{3}", text1) res2 = re.match("\w{4}", text2) print(res.group()) print(res1.group()) print(res2.group())
输出结果为
he
Hel
hellProcess finished with exit code 0
示例2:{m}
需求:匹配出,8到20位的密码,可以是大小写英文字母、数字、下划线
import re ret = re.match("[a-zA-Z0-9_]{6}","123a3g45678") print(ret.group()) ret = re.match("[[a-zA-Z0-9_]{8,20}","1ad3123456addfcasdef") print(ret.group())
#运行结果
123a3g
1ad3123456addfcasdef
5. {m,n}表示匹配m-n个字符 \w{2,4}表示匹配2-4个字符
import re text = "hello world" text1 = "Helloworld456" text2 = "hello world" res = re.match("\w{2,5}", text) res1 = re.match("\w{6,8}", text1) res2 = re.match("\w{20,25}", text2) print(res.group()) print(res1.group()) print(res2)
hello
Hellowor
NoneProcess finished with exit code 0
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 金池刘罡《昨日情歌HQ》头版限量编号[低速原抓WAV+CUE]
- 段奥娟.2024-23·岁碎念【白米范】【FLAC分轨】
- 顺子.2001-昨日·唯一·更多·顺子自选辑【魔岩】【WAV+CUE】
- 无印良品.1999-私藏三首·原创相对论(EP)【滚石】【WAV+CUE】
- Merryland《Merryland》[FLAC/分轨][170.22MB]
- 庄锭欣 ELKIE《没有你的未来》[320K/MP3][15.78MB]
- 庄锭欣 ELKIE《没有你的未来》[FLAC/分轨][36MB]
- TchaikovskySwanLake,Op.20柴可夫斯基天鹅湖(2013)SACD[Hi-Res]
- 24bit达人艺典《琴歌终极版》整轨WAV+CUE
- 黑鸭子.2005-《发烧DSD》柏菲[WAV分轨]
- 群星.1990-电影看板·人间道【宝丽金】【WAV+CUE】
- 周殷廷.2024-TO.WHOM.IT.MAY.CONCERN【环球】【FLAC分轨】
- 草蜢.1997-三人世界精选17首【宝丽金】【WAV+CUE】
- 【古琴音乐】管平湖《广陵散》2004[FLAC+CUE]
- 【中国民乐】《少数民族器乐·苗岭的早晨》2004[WAV+CUE]