python--re(匹配字符串)

d 匹配任何十进制数;它相当于类 [0-9]。
D 匹配任何非数字字符;它相当于类 [^0-9]。
s 匹配任何空白字符;它相当于类 [ fv]。
S 匹配任何非空白字符;它相当于类 [^ fv]。
w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。
W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]。
 
 *  匹配前面的子表达式零次或多次;{0,}
 + 匹配前面的子表达式一次或多次;{1,}
?匹配前面的子表达式零次或一次;{0,1}
import re
b = 3
'''
a = re.search('fa','fgafa')
 <_sre.SRE_Match object; span=(3, 5), match='fa'>
 a.group()#.group()返回匹配的字符串
 'fa'
 
match和search的区别
re.search(pattern, string, flags=0)
re.match(pattern, string, flags=0)
    match和search。match是从字符串的起点开始做匹配,而search(perl默认)是从字符串做任意匹配。
    re.match(pattern,string) = re.search(^pattern,string)
    
m = re.search('^ab','adff')
#None
m1 = re.search('^ab','abff')
#<_sre.SRE_Match object; span=(0, 2), match='ab'>
m2 = re.search('[^a-z]','dfafAAfa')
#<_sre.SRE_Match object; span=(4, 5), match='A'>
print(m1)
#<_sre.SRE_Match object; span=(0, 2), match='ab'>
print(m1.group())
#ab
a = re.match('as','sdaasff')
#None

#compile 编译正则表达式,返回RegexObject对象 re.compile(pattern, flags=0)
b = re.compile('ab')
result = b.match('abdfafssb')
print(result)
<_sre.SRE_Match object; span=(0, 2), match='ab'>
#re.split(pattern, string, maxsplit=0)
print(re.split('a','bakahha'))
['b', 'k', 'hh', '']
print(re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE))  #IGNORECASE忽略大小写
['0', '3', '9']
print(re.split('(W+)', '...words, words...') )
['', '...', 'words', ', ', 'words', '...', '']
print(re.split("a","bbb") )
['bbb']

#re.findall(pattern, string, flags=0)
print(re.findall(r"e","12a32bc43jf3") )
[]

print(re.findall(r"d+","12a32bc43jf3") )
s = re.findall(r"d+","12a32bc43jf3")
#print(s.group())
#['12', '32', '43', '3']
#返回的是一个列表
#re.finditer(pattern, string, flags=0)
it = re.finditer(r'd+','34hhj342kh24')
for match in it:
    print(match.group())
34
342
24
#re.sub(pattern, replace, string, count=0, flags=0)
re.compile是将正则表达式编译成一个对象,加快速度,并重复使用。
text = "JGood is a handsome boy, he is cool, clever, and so on..."
regex = re.compile(r'w*oow*')
print(regex.findall(text) )  #查找所有包含'oo'的单词
['JGood', 'cool']
print(regex.sub(lambda m: '[' + m.group(0) + ']', text) )#将字符串中含有'oo'的单词用[]括起来。
[JGood] is a handsome boy, he is [cool], clever, and so on...
'''
#re.sub(pattern, replace, string[, count, flags])
print(re.sub("d","RE","abc1def2hijk",))
abcREdefREhijk
print(re.subn("d","RE","abc1def2hijk",))
('abcREdefREhijk', 2)
原文地址:https://www.cnblogs.com/eilinge/p/9239150.html