在 Python中,我们可以使用内置的 re 模块来使用正则表达式。与大多数编程语言相同,正则表达式里使用‘\‘作为转义字符,这就可能造成反斜杠困扰。Python里的原生字符串很好地解决了这个问题,只需要在字符串前面加上‘r‘前缀。
使用 compile()
函数将正则表达式的字符串形式编译为一个 Pattern
对象
通过 Pattern
对象提供的一系列方法对文本进行匹配查找
import re # 将正则表达式编译成 Pattern对象,并指定匹配模式为点任意匹配模式 pattern = re.compile(r‘\d+‘,re.S)
match(string[, pos[, endpos]])
其中,string 是待匹配的字符串,pos 和 endpos 是可选参数,指定字符串的起始和终点位置,默认值分别是 0 和 len (字符串长度)。因此,当你不指定 pos 和 endpos 时,match 方法默认匹配字符串的头部。
In [1]: import re In [2]: pattern = re.compile(r"(\w+) (\d+)") In [3]: m = pattern.match(‘hello 123‘) In [4]: m.group(1) Out[4]: ‘hello‘ In [5]: m.group(1,2) Out[5]: (‘hello‘, ‘123‘) In [6]: m.group() Out[6]: ‘hello 123‘ In [7]: m.groups() Out[7]: (‘hello‘, ‘123‘) In [8]: m.start(1) Out[8]: 0 In [9]: m.start(2) Out[9]: 6 In [10]: m.end(1) Out[10]: 5 In [11]: m.span(1) Out[11]: (0, 5) In [12]: m.span(2) Out[12]: (6, 9)
search(string[, pos[, endpos]])
其中,string 是待匹配的字符串,pos 和 endpos 是可选参数,指定字符串的起始和终点位置,默认值分别是 0 和 len (字符串长度)。
>>> import re >>> pattern = re.compile(‘\d+‘) >>> m = pattern.search(‘one12twothree34four‘) # 这里如果使用 match 方法则不匹配 >>> m <_sre.SRE_Match object at 0x10cc03ac0> >>> m.group() ‘12‘ >>> m = pattern.search(‘one12twothree34four‘, 10, 30) # 指定字符串区间 >>> m <_sre.SRE_Match object at 0x10cc03b28> >>> m.group() ‘34‘ >>> m.span() (13, 15)
findall(string[, pos[, endpos]])
其中,string 是待匹配的字符串,pos 和 endpos 是可选参数,指定字符串的起始和终点位置,默认值分别是 0 和 len (字符串长度)。
import re #re模块提供一个方法叫compile模块,提供我们输入一个匹配的规则 #然后返回一个pattern实例,我们根据这个规则去匹配字符串 pattern = re.compile(r‘\d+\.\d*‘) #通过partten.findall()方法就能够全部匹配到我们得到的字符串 result = pattern.findall("123.141593, ‘bigcat‘, 232312, 3.15") #findall 以 列表形式 返回全部能匹配的子串给result for item in result: print(item)
运行结果:
123.141593 3.15
In [1]: import re In [2]: pattern = re.compile(r"\d+") In [3]: iter = pattern.finditer(‘hello123world456 haha789‘) In [4]: iter Out[4]: <callable_iterator at 0x7fb824fe2a90> In [5]: for m in iter: ...: print(m.group()) ...: 123 456 789
split(string[, maxsplit])
其中,maxsplit 用于指定最大分割次数,不指定将全部分割。
In [1]: import re In [2]: pattern = re.compile(r"[\d\s]") In [3]: pattern.split(‘hello1word2aaa bbb‘) Out[3]: [‘hello‘, ‘word‘, ‘aaa‘, ‘bbb‘] In [4]: pattern.split(‘hello1word2aaa bbb‘,2) Out[4]: [‘hello‘, ‘word‘, ‘aaa bbb‘]
sub(repl, string[, count])
其中,repl 可以是字符串也可以是一个函数:
In [1]: import re In [2]: pattern = re.compile(r‘\d+‘) In [3]: pattern.sub(‘100‘,‘hello20 world30‘)#将所有匹配到的数据替换成100 Out[3]: ‘hello100 world100‘ In [4]: pattern.sub(‘100‘,‘hello20 world30‘,1)#只替换第一个数据为100 Out[4]: ‘hello100 world30‘ In [5]: def add(temp): ...: ‘‘‘将匹配到的数据加1‘‘‘ ...: strNum = temp.group() ...: num = int(strNum)+1 ...: return str(num) In [6]: pattern.sub(add,‘hello20 world30‘)#将所有匹配到的数据加1 Out[6]: ‘hello21 world31‘ In [7]: pattern.sub(add,‘hello20 world30‘,1)#只将匹配到的第一个数据加1 Out[7]: ‘hello21 world30‘
In [1]: import re In [2]: pattern = re.compile(r‘\d+‘) In [3]: pattern.match(‘123456789‘).group() Out[3]: ‘123456789‘ In [4]: pattern = re.compile(r‘\d+?‘)#关闭贪婪模式 In [5]: pattern.match(‘123456789‘).group()#非贪婪模式下,?只匹配一个字符 Out[5]: ‘1‘ In [6]: pattern = re.compile(r‘<div>.*</div>‘) In [7]: pattern.match(‘<div>test1</div>bb<div>test2</div>‘).group() Out[7]: ‘<div>test1</div>bb<div>test2</div>‘ In [8]: pattern = re.compile(r‘<div>.*?</div>‘)#关闭贪婪模式 In [9]: pattern.match(‘<div>test1</div>bb<div>test2</div>‘).group() Out[9]: ‘<div>test1</div>‘
原文:https://www.cnblogs.com/Elite-Wang/p/14288152.html