python 正则表达式 re.search
生活随笔
收集整理的這篇文章主要介紹了
python 正则表达式 re.search
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 #coding:utf-8
2 import re
3
4 #將正則表達式編譯為pattern對象
5 #compile(pattern, flags=0)
6 #Compile a regular expression pattern, returning a pattern object.
7 pattern = re.compile(r'sub2020')
8 #help(re.match) Try to apply the pattern at the start of the string
9 #match 從 str 的開始(0 位置)查找匹配,如果沒有,返回none
10 #help(re.search) Scan through string looking for a match to the pattern
11 #search 掃描整個 str
12 match =re.search(pattern,'http://www.cnblogs.com/sub2020/p/7920845.html')
13
14 if match:
15 #使用match獲得group信息
16 print match.group()
17
18 m=re.search(r'(\w+)(\w+)(\w+)(\d+)(\d+)(\d+)(\d+)','http://www.cnblogs.com/sub2020/p/7920845.html')
19
20 #string: 匹配時使用的文本。
21 print 'm.string :',m.string
22 #re: 匹配時使用的Pattern對象
23 print 'm.re :',m.re
24 #pos: 文本中正則表達式開始搜索的索引。值與Pattern.match()
25 # 和Pattern.seach()方法的同名參數相同。
26 print 'm.pos :',m.pos
27 #endpos: 文本中正則表達式結束搜索的索引。值與Pattern.match()
28 # 和Pattern.seach()方法的同名參數相同。
29 print 'm.endpos :',m.endpos
30 #lastindex: 最后一個被捕獲的分組在文本中的索引。
31 # 如果沒有被捕獲的分組,將為None。
32 print 'm.lastindex :',m.lastindex
33 #lastgroup: 最后一個被捕獲的分組的別名。
34 # 如果這個分組沒有別名或者沒有被捕獲的分組,將為None。
35 print 'm.lastgroup :',m.lastgroup
36 #.group([group1, …]):獲得一個或多個分組截獲的字符串;
37 # 指定多個參數時將以元組形式返回。group1可以使用編號也可以使用別名;
38 # 編號0代表整個匹配的子串;不填寫參數時,返回group(0);
39 # 沒有截獲字符串的組返回None;截獲了多次的組返回最后一次截獲的子串。
40 print 'm.group() :',m.group()
41 print 'm.group(1,2) :',m.group(1,2)
42 print 'm.group(1, 2) :',m.group(1, 2)
43 #groups([default]):以元組形式返回全部分組截獲的字符串。
44 # 相當于調用group(1,2,…last)。default表示沒有截獲字符串的組以這個
45 # 值替代,默認為None。
46 print 'm.groups() :',m.groups()
47 print 'm.groups(3) :',m.groups(3)
48 #groupdict([default]):返回以有別名的組的別名為鍵、以該組截獲的子串
49 # 為值的字典,沒有別名的組不包含在內。default含義同上。
50 print 'm.groupdict() :',m.groupdict()
51 #start([group]):返回指定的組截獲的子串在string中的起始索引
52 #(子串第一個字符的索引)。group默認值為0。
53 print 'm.start(2) :',m.start(2)
54 print 'm.start(3) :',m.start(3)
55 #end([group]):返回指定的組截獲的子串在string中的結束索引
56 #(子串最后一個字符的索引+1)。group默認值為0。
57 print 'm.end(2) :',m.end(2)
58 print 'm.end(3) :',m.end(3)
59 #span([group]):返回(start(group), end(group))。
60 print 'm.span(1) :',m.span(1)
61 print 'm.span(2) :',m.span(2)
62 #expand(template):將匹配到的分組代入template中然后返回。
63 # template中可以使用\id或\g、\g引用分組,但不能使用編號0。
64 # \id與\g是等價的;但\10將被認為是第10個分組,如果你想表達\1之后是字符’0’,
65 # 只能使用\g0。
66 print r"m.expand(r'\g') 1:",m.expand(r'\1')
67 print r"m.expand(r'\g') 2:",m.expand(r'\2')
68 print r"m.expand(r'\g') 3:",m.expand(r'\3')
69
70 print r"m.expand(r'\g \g\g') :",m.expand(r'\2 \2\2')
71 print r"m.expand(r'\g \g\g') :",m.expand(r'\2 \1\3')
Output:
1 sub2020 2 m.string : http://www.cnblogs.com/sub2020/p/7920845.html 3 m.re : <_sre.SRE_Pattern object at 0x0000000001D08810> 4 m.pos : 0 5 m.endpos : 45 6 m.lastindex : 7 7 m.lastgroup : None 8 m.group() : sub2020 9 m.group(1,2) : ('s', 'u') 10 m.group(1, 2) : ('s', 'u') 11 m.groups() : ('s', 'u', 'b', '2', '0', '2', '0') 12 m.groups(3) : ('s', 'u', 'b', '2', '0', '2', '0') 13 m.groupdict() : {} 14 m.start(2) : 24 15 m.start(3) : 25 16 m.end(2) : 25 17 m.end(3) : 26 18 m.span(1) : (23, 24) 19 m.span(2) : (24, 25) 20 m.expand(r'\g') 1: s 21 m.expand(r'\g') 2: u 22 m.expand(r'\g') 3: b 23 m.expand(r'\g \g\g') : u uu 24 m.expand(r'\g \g\g') : u sb 25 26 ***Repl Closed***quote:http://cuiqingcai.com/977.html
轉載于:https://www.cnblogs.com/sub2020/p/7921152.html
總結
以上是生活随笔為你收集整理的python 正则表达式 re.search的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ1015
- 下一篇: 搜索引擎大调整:百度出“惊雷算法”后36