Function of the re module
compile (pattern): create schema objects
import re
pat=re.compile('A')
m=pat.search('CBA') #等价于 re.search('A','CBA')
print m
<_sre.SRE_Match object at 0x9d690c8> #匹配到了,返回MatchObject(True)
m=pat.search('CBD')
print m
None #没有匹配到,返回None(False)
search (pattern, string): look for patterns in strings
m = re.search('asd','ASDasd')
print m
<_sre.SRE_Match object at 0xb72cd6e8> #匹配到了,返回MatchObject(True)
m = re.search('asd','ASDASD')
print m
None #没有匹配到,返回None(False)
match (pattern, string): at the beginning of the string matching pattern
m = re.search('asd','ASDasd')
print m
<_sre.SRE_Match object at 0xb72cd6e8> #匹配到了,返回MatchObject(True)
m = re.search('asd','ASDASD')
print m
None #没有匹配到,返回None(False)
Equivalent to
pat=re.compile('a')
print pat.match('Aasd')
None
printpat.match('aASD')
<_sre.SRE_Match object at 0xb72cd6e8>
The above function can be determined returns if conditional statement:
if pat.search('asd'):
... print 'OK'
...
OK #找到返回
if re.search('a','ASD'):
... print "OK"
... #没有找到
split (pattern, string): Split string according to the mode, returns a list of
re.split(',','a,s,d,asd')
['a', 's', 'd', 'asd'] #返回列表
pat = re.compile(',')
pat.split('a,s,d,asd')
['a', 's', 'd', 'asd'] #返回列表
re.split('[, ]+','a , s ,d ,,,,,asd') #正则匹配:[, ]+,后面说明
['a', 's', 'd', 'asd']
re.split('[, ]+','a , s ,d ,,,,,asd',maxsplit=2) # maxsplit 最多分割次数
['a', 's', 'd ,,,,,asd']
pat = re.compile('[, ]+') #正则匹配:[, ]+,后面说明
pat.split('a , s ,d ,,,,,asd',maxsplit=2) # maxsplit 最多分割次数
['a', 's', 'd ,,,,,asd']
findall (pattern, string): Returns a list of matches
re.findall('a','ASDaDFGAa')
['a', 'a'] #列表形式返回匹配到的字符串
pat = re.compile('a')
pat.findall('ASDaDFGAa')
['a', 'a'] #列表形式返回匹配到的字符串
pat = re.compile('[A-Z]+') #正则匹配:'[A-Z]+' 后面有说明
pat.findall('ASDcDFGAa')
['ASD', 'DFGA'] #找到匹配到的字符串
pat = re.compile('[A-Z]')
pat.findall('ASDcDFGAa') #正则匹配:'[A-Z]+' 后面有说明
['A', 'S', 'D', 'D', 'F', 'G', 'A'] #找到匹配到的字符串
pat = re.compile('[A-Za-z]') #正则匹配:'[A-Za-z]+' 匹配所有单词,后面有说明
pat.findall('ASDcDFGAa')
['A', 'S', 'D', 'c', 'D', 'F', 'G', 'A', 'a']
sub (pat, repl, string): Alternatively pat matches with repl
(Stay in the middle because the middle in the center)
re.sub('a','A','abcasd') #找到a用A替换,后面见和group的配合使用
'AbcAsd'
pat = re.compile('a')
pat.sub('A','abcasd')
'AbcAsd'
pat=re.compile(r'www\.(.*)\..{3}') #正则表达式
#在Python的string前面加上‘r’, 是为了告诉编译器这个string是个raw string,不要转译反斜杠 '\' 。
#例如,\n 在raw string中,是两个字符,\和n, 而不会转译为换行符。
#由于正则表达式和 \ 会有冲突,因此,当一个字符串使用了正则表达式后,最好在前面加上'r'。
#与大多数编程语言相同,正则表达式里使用"\"作为转义字符,这就可能造成反斜杠困扰。
#假如你需要匹配文本中的字符"\",那么使用编程语言表示的正则表达式里将需要4个反斜杠"\\\\":
#前两个和后两个分别用于在编程语言里转义成反斜杠,转换成两个反斜杠后再在正则表达式里转义成一个反斜杠。
#Python里的原生字符串很好地解决了这个问题,这个例子中的正则表达式可以使用r"\\"表示。
#同样,匹配一个数字的"\\d"可以写成r"\d"。
#有了原生字符串,你再也不用担心是不是漏写了反斜杠,写出来的表达式也更直观。
#不是说 加了r \就没有转译功能,好乱,就直接记住1句话:
#当一个字符串使用了正则表达式后,最好在前面加上'r',这样你再也不用担心是不是漏写了反斜杠,写出来的表达式也更直观
pat.match('www.dxy.com').group(1)
'dxy'
re.sub(r'www\.(.*)\..{3}',r'\1','hello,www.dxy.com')
pat.sub(r'\1','hello,www.dxy.com')
'hello,dxy'
# r'1' 是第一组的意思
#通过正则匹配找到符合规则的"www.dxy.com" ,取得 组1字符串 去替换 整个匹配。
pat=re.compile(r'(\w+) (\w+)') #正则表达式
s='hello world ! hello hz !'
pat.findall('hello world ! hello hz !')
[('hello', 'world'), ('hello', 'hz')]
pat.sub(r'\2 \1',s) #通过正则得到组1(hello),组2(world),再通过sub去替换。即组1替换组2,组2替换组1,调换位置。
'world hello!hz hello!'
escape (string): The string inside special escape
re.escape('www.dxy.cn')
'www\\.dxy\\.cn' #转义
Above function, only the match, search methods have group, no other functions.
Method function
group: acquisition sub-mode (group) match
pat = re.compile(r'www\.(.*)\.(.*)') #用()表示1个组,2个组
m = pat.match('www.dxy.com')
m.group() #默认为0,表示匹配整个字符串
'www.dxy.com'
m.group(1) #返回给定组1匹配的子字符串
'dxy'
m.group(2)
'com'
start: the start position of a given group matches
m.start(2) #组2开始的索引
8
end: a given group matches end position
m.end(2) #组2结束的索引
11
span: the beginning of a given set of matches to end position
m.span(2) #组2开始、结束的索引
(8, 11)
Regular Expressions
Metacharacters
. "": Wildcard, except for newline characters in any one
pat=re.compile('.')
pat.match('abc')
<_sre.SRE_Match object at 0xb72b6170>
pat.match('abc').group()
'a' #匹配到了首个字符
pat.search('abc').group()
'a'
pat.match('\n').group() #换行符匹配出错
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
"\" : Escapes
pat=re.compile('\.')
pat.search('abc.efg').group() #匹配到.
'.'
pat.findall('abc.efg') #不用group,返回列表
['.']
"[...]": set of characters matches any element inside
>>> pat=re.compile('[abc]')
>>> pat.match('axbycz').group()
'a'
>>> pat.search('axbycz').group()
'a'
>>> pat.findall('axbycz')
['a', 'b', 'c']
"\ D": Digital
>>> pat=re.compile('\d')
>>> pat.search('ax1by2cz3').group() #匹配到第一个数字:1,返回
'1'
>>> pat.match('ax1by2cz3').group() #匹配不到(首个不是)返回None,报错,match匹配字符串头
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> pat.findall('ax1by2cz3') #匹配所有的数字,列表返回
['1', '2', '3']
"\ D": non-numeric
>>> pat=re.compile('\D')
>>> pat.match('ax1by2cz3').group()
'a'
>>> pat.search('ax1by2cz3').group()
'a'
>>> pat.findall('ax1by2cz3')
['a', 'x', 'b', 'y', 'c', 'z']
"\ S": a blank character, \ t, \ r, \ n, space
>>> pat=re.compile('\s')
>>> pat.findall('\rax1 \nby2 \tcz3')
['\r', ' ', '\n', ' ', '\t']
>>> pat.search('\rax1 \nby2 \tcz3').group()
'\r'
>>> pat.match('\rax1 \nby2 \tcz3').group()
'\r'
"S": non-whitespace characters
>>> pat=re.compile('\S')
>>> pat.search('\rax1 \nby2 \tcz3').group()
'a'
>>> pat.findall('\rax1 \nby2 \tcz3')
['a', 'x', '1', 'b', 'y', '2', 'c', 'z', '3']
"\ W": single numbers and letters, [A-Za-z0-9]
>>> pat=re.compile('\w')
>>> pat.search('1a2b3c').group()
'1'
>>> pat.findall('1a2b3c')
['1', 'a', '2', 'b', '3', 'c']
>>> pat.match('1a2b3c').group()
'1'
"\ W": non-word character, in addition to numbers and letters
>>> pat=re.compile('\W')
>>> pat.findall('1a2我b3c') #python是用三字节表示一个汉字
['\xe6', '\x88', '\x91']
>>> pat.search('1a2我b3c').group()
'\xe6'
Quantifier
"*": 0 or more times
(Take 0 will become 0)
>>> pat = re.compile('[abc]*')
>>> pat.match('abcabcdefabc').group()
'abcabc' #2次
>>> pat.search('abcabcdefabc').group()
'abcabc' #2次
>>> pat.findall('abcabcdefabc')
['abcabc', '', '', '', 'abc', ''] #2次和1次,因为有0次,所以匹配了''
"+": One or more times
(Plus 0 does not become 0)
>>> pat = re.compile('[abc]+')
>>> pat.match('abcdefabcabc').group()
'abc'
>>> pat.search('abcdefabcabc').group()
'abc'
>>> pat.findall('abcdefabcabc')
['abc', 'abcabc']
"?": 0 or 1, match, search will not be none, there will be '' (because 0 is in line with the times)
0 or 1 does not refer to [xxx] this set, but in which any character
>>> pat = re.compile('[abc]?')
>>> pat.match('defabc').group() #0次
''
>>> pat.match('abcdefabc').group()
'a'
>>> pat.search('defabc').group() #0次
''
>>> pat.findall('defabc') #0次和1次
['', '', '', 'a', 'b', 'c', ''] #后面总再加个''
"Quantifier?": Non-greedy mode: match only minimal (as little as possible); default greedy mode: matching up (as much)
>>> pat = re.compile('[abc]+') #贪婪模式
>>> pat.match('abcdefabcabc').group() #匹配尽可能多的:abc
'abc'
>>> pat.match('bbabcdefabcabc').group()
'bbabc'
>>> pat.search('dbbabcdefabcabc').group()
'bbabc'
>>> pat.findall('abcdefabcabc')
['abc', 'abcabc']
>>> pat = re.compile('[abc]+?') #非贪婪模式:+?
>>> pat.match('abcdefabcabc').group() #匹配尽可能少的:a、b、c
'a'
>>> pat.search('dbbabcdefabcabc').group()
'b'
>>> pat.findall('abcdefabcabc')
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
"{M}": string match occurs m times
>>> pat = re.compile('[op]{2}') #o或p出现2次
>>> pat.search('abcooapp').group() #匹配第一次出现的字符串,o比p先出现
'oo'
>>> pat.findall('abcooapp') #匹配出现的所有字符串,列表形式返回
['oo', 'pp']
"{M, n}": matching string appears to n times m
>>> pat = re.compile('[op]{2,4}') #o或则p出现2到4次
>>> pat.match('pppabcooapp').group() #匹配开头
'ppp'
>>> pat.search('pppabcooapp').group() #匹配第一次出现
'ppp'
>>> pat.findall('pppabcooapp') #匹配所有
['ppp', 'oo', 'pp']
.group () # match for the first time *
boundary
"^": The beginning of the match string or line head
>>> pat = re.compile('^[abc]') #开头是a、b、c中的任意一个
>>> pat.search('defabc').group()
>>> pat.match('defabc').group() #均找不到
>>> pat.findall('defabc')
[]
>>> pat.search('adefabc').group()
'a'
>>> pat.match('adefabc').group() #开头是a、b、c中的任意一个
'a'
>>> pat.findall('adefabc')
['a']
>>> pat = re.compile('^[abc]+') #开头是a、b、c中的任意一个的一次或则多次,贪婪:匹配多个
>>> pat.findall('cbadefab')
['cba']
>>> pat = re.compile(r'^[abc]+?') #开头是a、b、c中的任意一个的一次或则多次,非贪婪:匹配一个
>>> pat.findall('cbadefab')
['c']
"$": End of the string or the end of the line
>>> pat = re.compile('[abc]$')
>>> pat.match('adefAbc').group() #match匹配的是字符串开头,所以查找$的时,总是返回None
>>> pat.search('adefAbc').group() #结尾是a、b、c中的任意一个
'c'
>>> pat.findall('adefAbc')
['c']
>>> pat = re.compile('[abc]+$')
>>> pat.search('adefAbc').group() #结尾是a、b、c中的任意一个的一次或则多次,贪婪:匹配多个
'bc'
>>> pat.findall('adefAbc')
['bc']
"\ A": start of the string
>>> pat = re.compile('\A[abc]+')
>>> pat.findall('cbadefab')
['cba']
>>> pat.search('cbadefab').group()
'cba'
"\ Z": end of the string
>>> pat = re.compile('[abc]+\Z')
>>> pat.search('cbadefab').group()
'ab'
>>> pat.findall('cbadefab')
['ab']
Packet
(...): matched packet, from left to right, each encounters a (number plus one, the packet can be added later quantifier
>>> pat=re.compile(r'(a)\w(c)') #\w:单个的数字或字母 [A-Za-z0-9]
>>> pat.match('abcdef').group()
'abc'
>>> pat=re.compile('(a)b(c)') #分2组,匿名分组
>>> pat.match('abcdef').group() #默认返回匹配的字符串
'abc'
>>> pat.match('abcdef').group(1) #取分组1,适用于search
'a'
>>> pat.match('abcdef').group(2) #取分组2,适用于search
'c'
>>> pat.match('abcdef').groups() #取所有分组,元组形式返回
('a', 'c')
: Reference number for the packet is matched to a string
>>> pat=re.compile(r'www\.(.*)\..{3}')
>>> pat.match('www.dxy.com').group(1)
'dxy'
* "(? P ...)": Inside mode with () to represent the group (group name) for the extraction of some of the target string parts.
>>> pat=re.compile(r'(?P<K>a)\w(c)') #分2组:命名分组+匿名分组
>>> pat.search('abcdef').groups() #取所有分组,元组形式返回
('a', 'c')
>>> pat.search('abcdef').group(1) #取分组1,适用于match
'a'
>>> pat.search('abcdef').group(2) #取分组2,适用于match
'c'
>>> pat.search('abcdef').group() #默认返回匹配的字符串
'abc'
>>> pat.search('abcdef').groupdict() #命名分组可以返回一个字典【专有】,匿名分组也没有
{'K': 'a'}
"(? P = name)": a reference to the packet matches the alias string
>>> pat=re.compile(r'(?P<K>a)\w(c)(?P=K)') #(?P=K)引用分组1的值,就是a
>>> pat.search('abcdef').group() #匹配不到,因为完整'a\wca',模式的第4位是a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'group'
>>> pat.search('abcadef').group() #匹配到,模式的第4位和组1一样,值是c
'abca'
>>> pat.search('abcadef').groups()
('a', 'c')
>>> pat.search('abcadef').group(1)
'a'
>>> pat.search('abcadef').group(2)
'c
"": A reference group number matches:
>>> pat=re.compile(r'(?P<K>a)\w(c)(?P=K)\2') #\2引用分组2的值,就是c
>>> pat.findall('Aabcadef') #匹配不到,因为完整'a\wcac',模式的第5位是c
[]
>>> pat.findall('Aabcacdef') #匹配到,模式的第5位和组2一样,值是c
[('a', 'c')]
>>> pat.search('Aabcacdef').groups()
('a', 'c')
>>> pat.search('Aabcacdef').group()
'abcac'
>>> pat.search('Aabcacdef').group(1)
'a'
>>> pat.search('Aabcacdef').group(2)
'c'
Special construction
(?:…) (…)不分组版本,用于使用 | 或者后接数量词
(?iLmsux) iLmsux的每个字符代表一个匹配模式,只能用在正则表达式的开头,可选多个
(?#…) #号后的内容将作为注释
(?=…) 之后的字符串内容需要匹配表达式才能成功匹配
(?!…) 之后的字符串不匹配表达式才能成功
(?(?(?(id/name) yes |no) 如果编号为id/名字为name的组匹配到字符串,则需要匹配yes,否则匹配no,no可以省略
"(:? ...)" :() there are:? Indicates that the () is not a grouping
>>> pat=re.compile(r'a(?:bc)')
>>> pat.findall('abc')
['abc']
>>> pat.match('abc').groups() #显示不出分组
"(? = ...)": Matches ... expression that returns. After matching, always on the back to match
>>> pat=re.compile(r'\w(?=\d)') #匹配表达式\d,返回数字的前一位,\w:单词字符[A-Za-z0-9]
>>> pat.findall('abc1 def1 xyz1')
['c', 'f', 'z']
>>> pat.findall('zhoujy20130628hangzhou') #匹配数字的前一位,列表返回
['y', '2', '0', '1', '3', '0', '6', '2']
>>> pat=re.compile(r'\w+(?=\d)')
>>> pat.findall('abc1,def1,xyz1') #匹配最末数字的前字符串,列表返回
['abc', 'def', 'xyz']
>>> pat.findall('abc21,def31,xyz41')
['abc2', 'def3', 'xyz4']
>>> pat.findall('zhoujy20130628hangzhou')
['zhoujy2013062']
>>> pat=re.compile(r'[A-Za-z]+(?=\d)') #[A-Za-z],匹配字母,可以用其他的正则方法
>>> pat.findall('zhoujy20130628hangzhou123') #匹配后面带有数字的字符串,列表返回
['zhoujy', 'hangzhou']
>>> pat.findall('abc21,def31,xyz41')
['abc', 'def', 'xyz']
"(?! ...)" ... does not match the expression returns. After matching
>>> pat=re.compile(r'[A-Za-z]+(?!\d)') #[A-Za-z],匹配字母,可以用其他的正则方法
>>> pat.findall('zhoujy20130628hangzhou123,12,binjiang310') #匹配后面不是数字的字符串,列表返回
['zhouj', 'hangzho', 'binjian']
>>> pat.findall('abc21,def31,xyz41')
['ab', 'de', 'xy']
"(? <= ...)": Matches ... expression that returns. The former match, always on the front match
>>> pat=re.compile(r'(?<=\d)[A-Za-z]+') #匹配前面是数字的字母
>>> pat.findall('abc21,def31,xyz41')
[]
>>> pat.findall('1abc21,2def31,3xyz41')
['abc', 'def', 'xyz']
>>> pat.findall('zhoujy20130628hangzhou123,12,binjiang310')
['hangzhou']
“(?
>>> pat=re.compile(r'(?<!\d)[A-Za-z]+') #匹配前面不是数字的字母
>>> pat.findall('abc21,def31,xyz41')
['abc', 'def', 'xyz']
>>> pat.findall('zhoujy20130628hangzhou123,12,binjiang310')
['zhoujy', 'angzhou', 'binjiang']
"(? (Id / name) yes | no)": group matches, the return match
>>> pat=re.compile(r'a(\d)?bc(?(1)\d)') #no省略了,完整的是a\dbc\d ==> a2bc3,总共5位,第2位是可有可无的数字,第5为是数字
>>> pat.findall('abc9') #返回组1,但第2位(组1)没有,即返回了''
['']
>>> pat.findall('a8bc9') #完整的模式,返回组1
['8']
>>> pat.match('a8bc9').group()
'a8bc9'
>>> pat.match('a8bc9').group(1)
'8'
>>> pat.findall('a8bc') #第5位不存在,则没有匹配到
[]
"(? ILmsux)": Here i will introduce the next parameters: case sensitive matching
>>> pat=re.compile(r'abc')
>>> pat.findall('abc')
['abc']
>>> pat.findall('ABC')
[]
>>> pat=re.compile(r'(?i)abc') #(?i) 不区分大小写
>>> pat.findall('ABC')
['ABC']
>>> pat.findall('abc')
['abc']
>>> pat.findall('aBc')
['aBc']
>>> pat.findall('aBC')
['aBC']
>>> pat=re.compile(r'abc',re.I) #re.I 作为参数使用,推荐
>>> pat.findall('aBC')
['aBC']
>>> pat.findall('abc')
['abc']
>>> pat.findall('ABC')
['ABC']
Source: chichao
segmentfault.com/a/1190000011578899