首页 > 代码库 > 【小甲鱼】【Python】正则表达式(二)

【小甲鱼】【Python】正则表达式(二)

 

 1 >>> import re
#|表示或的意思
2 >>> re.search(r"Fish(C|D)","FishC") 3 <_sre.SRE_Match object; span=(0, 5), match=FishC> 4 >>> re.search(r"Fish(C|D)","FishE") 5 >>> re.search(r"^FishC","Fish") 6 >>> re.search(r"^FishC","FishC dfadf") 7 <_sre.SRE_Match object; span=(0, 5), match=FishC>
#^表示以什么开头 8 >>> re.search(r"^FishC","I FishC dfadf")
#$表示以什么结尾
9 >>> re.search(r"FishC$","I FishC dfadf") 10 >>> re.search(r"FishC$","I FishC dfadfFishC") 11 <_sre.SRE_Match object; span=(13, 18), match=FishC>
#表示查找回车 12 >>> re.search(r"[\n]","I FishC dfadfFishC\n") 13 <_sre.SRE_Match object; span=(18, 19), match=\n>
#[^...]表示查找除了...条件外的数据 14 >>> re.search(r"[^a-z]","I FishC dfadfFishC\n") 15 <_sre.SRE_Match object; span=(0, 1), match=I>
#[^...]表示查找...条件的数据
16 >>> re.search(r"[a-z^]","I FishC dfadfFishC\n")
17 <_sre.SRE_Match object; span=(3, 4), match=i>
18 >>> re.findall(r"[a-z^]","I FishC dfadfFishC\n")
19 [i, s, h, d, f, a, d, f, i, s, h]
20 >>> re.findall(r"[^a-z]","I FishC dfadfFishC\n")
21 [I, , F, C, , F, C, \n]
22 #\1表示重复一次
23 >>> re.search(r"(FishC)\1","FishCFishC")
24 <_sre.SRE_Match object; span=(0, 10), match=FishCFishC>
25 #141是八进制的a(十进制ASCII值为97)值
26 >>> re.search(r"(FishC)\141","FishCFishC")
27 >>> re.search(r"(FishC)\141","FishCFishCa")
28 <_sre.SRE_Match object; span=(5, 11), match=FishCa>
29 >>> re.search(r"(FishC)\060","FishCFishCa")
30 >>> re.search(r"(FishC)\060","FishCFishC0")
31 <_sre.SRE_Match object; span=(5, 11), match=FishC0>
32 >>> re.search(r"\.","FishCFishC0.com")
33 <_sre.SRE_Match object; span=(11, 12), match=.>
34 >>> re.search(r"[.]","FishCFishC0.com")
35 <_sre.SRE_Match object; span=(11, 12), match=.>
36 >>> re.search(r"C*","FishCFishC0.com")
37 <_sre.SRE_Match object; span=(0, 0), match=‘‘>
38 #*表示重复无限次
39 >>> re.search(r"(FishC)*","FishCFishC0.com")
40 <_sre.SRE_Match object; span=(0, 10), match=FishCFishC>
41 #+表示重复1到无限次
42 >>> re.search(r"(FishC)+","FishCFishC0.com")
43 <_sre.SRE_Match object; span=(0, 10), match=FishCFishC>
44 #?表示重复0或1次
45 >>> re.search(r"(FishC)?","FishCFishC0.com")
46 <_sre.SRE_Match object; span=(0, 5), match=FishC>
47 >>> s="<html><title>I love FishC.com</title></html>"
48 >>> re.search(r"<.+>",s)
49 <_sre.SRE_Match object; span=(0, 44), match=<html><title>I love FishC.com</title></html>>
50 >>> re.search(r"<.+?>",s)
51 <_sre.SRE_Match object; span=(0, 6), match=<html>>

 

【小甲鱼】【Python】正则表达式(二)