import re
https://docs.python.org/3.7/library/re.html
https://www.cnblogs.com/tina-python/p/5508402.html
/*******************************[] Used to indicate a set of characters. In a set:以下是集合的几种用法*******************************/
字符的范围可以通过给出两个字符并用'-'分隔它们来表示,例如[a-z]将匹配任何小写ASCII字母,[0-5][0-9]将匹配从00到59的所有两位数字
text = "He was carefully disguised but captured quickly by police."
result = re.findall(r"l[a-z]", text)
print(result) #返回结果:['ll', 'ly', 'li']
字符可以单独列出,例如[lmy]将匹配'l'、'm'或'y'
text = "He was carefully disguised but captured quickly by police."
result = re.findall(r"l[lmy]", text)
print(result) #返回结果:['ll', 'ly']
If - is escaped (e.g. [a-z]) or if it’s placed as the first or last character (e.g. [-a] or [a-]), it will match a literal '-'.
text = "He was carefully disguised but captured quickly by police l-0, l9."
result = re.findall(r"l[-9]", text)
print(result) #返回结果:['l-', 'l9']
特殊字符在集合中失去其特殊意义。例如,[(+)]将匹配任何文字字符'(','+','',或')'
text = "He was carefully disguised but captured quickly by police l+0, l* l(."
result = re.findall(r"l[(+)]", text)
print(result) #返回结果:['l+', 'l', 'l(']
字符类(如\w或\S(定义如下))也可以在一个集合中接受,尽管它们匹配的字符取决于ASCII或LOCALE模式是否有效。
text = "He was carefully disguised but captured quickly by police fastlyy"
result = re.findall(r"\w+ly", text)
print(result) #返回结果:['carefully', 'quickly', 'fastly']
可以通过补充该组来匹配不在范围内的字符。 如果集合的第一个字符是“^”,则将匹配集合中不包含的所有字符。
例如,[^ 5]将匹配除“5”之外的任何字符,[^]将匹配除“”之外的任何字符。 如果它不是集合中的第一个字符,则没有特殊含义。
text = "He was carefully disguised but captured quickly by police fastlyy"
result = re.findall(r"\w+[^l]y", text)
print(result) #返回结果:['fastlyy']
要匹配集合中的文字“]',请在其前面加上反斜杠,或将其放在集合的开头。 例如,[()[] {}]和[]()[{}]都将匹配括号。
text = "He was carefully disguised but captured quickly by police [fastlyy]"
result = re.findall(r"\w+[]]", text)
print(result) #返回结果:['fastlyy]']
. 在默认模式下,它匹配除换行符之外的任何字符。如果指定了DOTALL标志,则匹配包括换行符在内的任何字符。
text = "He was carefully disguised but captured quickly by police fastlyy"
result = re.findall(r"g.i", text)
print(result) #返回结果:['gui']
^ 匹配字符串的开头,在多行模式下,也在每个换行之后立即匹配。
text = "He was carefully disguised but captured quickly by police fastlyy"
result = re.findall(r"^He was", text)
print(result) #返回结果:['He was']
result = re.findall(r"^carefully", text)
print(result) #返回结果:[]
$ 匹配字符串末尾,在多行模式中匹配每一行的末尾
text = "He was carefully disguised but captured quickly by police fastlyy foo and foobar"
result = re.findall(r"foobar$", text)
print(result) #返回结果:['foobar']
? 匹配一个字符0次或1次
text = "He food foo and foob and foobbbb"
result = re.findall(r"foob?", text)
print(result) #返回结果:['foo', 'foo', 'foob', 'foob']
* 匹配前一个字符0或多次
text = "He food foo and foob and foobbbb"
result = re.findall(r"foob*", text)
print(result) #返回结果:['foo', 'foo', 'foob', 'foobbbb']
+ 匹配前一个字符1次或无限次
text = "He food foo and foob and foobbbb"
result = re.findall(r"foob+", text)
print(result) #返回结果:['foob', 'foobbbb']
text = "com/sz300270.html"
result = re.findall(r"com/\S\S(.*?).html", text)
print(result) #返回结果:['300270']
text = "<a> b <c>"
result = re.findall(r"<.>", text)
print(result) #返回结果:['<a>', '<c>']
text = "<a> b <c>"
result = re.findall(r"<.*>", text)
print(result) #返回结果:<a> b <c>
text = "<a> b <c>"
result = re.findall(r"<.*?>", text)
print(result) #返回结果:['<a>', '<c>']
text = "<a> b <c>"
result = re.findall(r"<(.*?)>", text)
print(result) #返回结果:['a', 'c']
\d 数字:[0-9]
text = "abc a1c a2c"
result = re.findall(r"a\dc", text)
print(result) #返回结果:['a1c', 'a2c']
\D 非数字
text = "abc a1c a2c"
result = re.findall(r"a\Dc", text)
print(result) #返回结果:['abc']
\S 匹配任何非空白字符
text = "abc a1c a c a_c"
result = re.findall(r"a\Sc", text)
print(result) #返回结果:['abc', 'a1c', 'a_c']
\s 匹配任何空白字符
text = "abc a1c a c a b"
result = re.findall(r"a\sc", text)
print(result) #返回结果:['a c']
\w 匹配包括下划线在内的任何字字符 ??????
text = "abdc a1c a c a_b"
result = re.findall(r"a\wc", text)
print(result) #返回结果:['a1c']
\W 匹配非字母字符,即匹配特殊字符
text = "abdc a1c a c a_b"
result = re.findall(r"a\Wc", text)
print(result) #返回结果:['a c']
print(re.findall(r'\d+','12 drumm44ers drumming, 11 ... 100 ...'))
iter = re.finditer(r'\d+','12 drumm44ers drumming, 11 ... 100 ...')
for i in iter:
print(i)
print(i.group())
print(i.span())
print(re.split('\d+','one1two2three3four4five5'))
print(re.split(r'|','one|two|three'))
text = "JGood is a handsome boy, he is cool, clever, and so on..."
print(re.sub(r'\s+', '-', text))
text = "JGood is a handsome boy, he is cool, clever, and so on..."
print(re.sub(r'\s+', lambda m:'['+m.group(0)+']', text,0))
print(re.subn('[1-2]','A','123456abcdef'))
print(re.sub("g.t","have",'I get A, I got B ,I gut C'))
print(re.subn("g.t","have",'I get A, I got B ,I gut C'))
例子:<Module>.<string>(.U) 替换为 "???"