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) 替換為 "???"