python正则表达式

. 匹配任意单个字符

>>> re.findall('o.','hello world')
['o ', 'or']

[] 匹配指定的单个字符

>>> re.findall('[lw]o','hello world')
['lo', 'wo']

[a-z] 匹配a到z的任意单个字母

>>> re.findall('o[a-z]','hello world')
['or']

.*? 匹配任意长度的任意字符

>>> re.findall('h.*?d','hello world')
['hello world']

() 可用于截取查询结果中的任意一段

>>> re.findall(' .. ', 'hello my world')
[' my ']
>>> re.findall(' (..) ', 'hello my world')
['my']

* 可用于匹配任意数量的重复字符,包括零个、一个和多个
例如 *o 可匹配 '', 'o', 'oo', 'ooo' ... ...

>>> re.findall('o*ps',' ps ops oops oooooops')
['ps', 'ops', 'oops', 'oooooops']

+* 作用类似,但不匹配空字符

>>> re.findall('o+ps',' ps ops oops oooooops')
['ops', 'oops', 'oooooops']

{} 可匹配指定数量的重复字符

>>> re.findall('o{2}ps',' ps ops oops oooooops')
['oops', 'oops']

| 表示匹配条件中的“或”关系

>>> re.findall('to|be','to be or not to be')
['to', 'be', 'to', 'be']

d 可用于匹配单个数字

>>> re.findall('d','hello 2020')
['2', '0', '2', '0']
原文地址:https://www.cnblogs.com/bill-h/p/13909404.html