" \ +數字 "
(1)表示第n個元組中的內容
(bca)\1==bcabca;xu_hao_1=re.search(r'a(bca)\1'
,'abcabca'
(xu_hao_1)
#abcabca
(2)數字為三位數時,表示該八進位制數對應ascii碼的內容xu_hao_2=re.search(r'(a)(bca)\2'
,'abcabca'
(xu_hao_2)
#abcabca
xu_hao_3=re.search(r'(abc)\1\1'
,'abcabcabc'
(xu_hao_3)
#abcabcabc
xu_hao_4=re.search(r'(abc)\1'
,'abcabcabc'
(xu_hao_4)
''' #abcabc
" . "xu_hao_5=re.search(r'\141'
,'abc'
(xu_hao_5)
#axu_hao_6=re.search(r'\061'
,'123'
(xu_hao_6)
#1
(1)不帶方括號,表示第乙個字元
(2)帶方括號,表示"."本身`point1=re.search(r'.'
,'the first letter'
(point1)
# t
" [ ] "point2=re.search(r'[.]'
,'the . itself'
(point2)
# .
(1)表示範圍
(2)方括號中 " ^ "的用法fang_kuo_hao_1=re.findall(r'[a-z]'
,'get all letters i need'
(fang_kuo_hao_1)
#['e', 't', 'l', 'l', 'e', 't', 't', 'e', 'r', 's', 'e', 'e', 'd']
放在前面表示提取除了該內容之外的fang_kuo_hao_2=re.findall(r'[^(a-z)]'
,"get all letter i don't need"
(fang_kuo_hao_2)
#['g', ' ', 'a', ' ', 'l', ' ', 'i', ' ', 'd', "'", ' ', 'n']
放在後面表示 " ^ "本身fang_kuo_hao_3=re.findall(r'[(a-z)^]'
,'no change ^'
(fang_kuo_hao_3)
#['o', 'h', 'a', 'n', 'g', 'e', '^']
重複 " " " ? " " * " " + "
(1) " "
abc==abccrepeat1=re.search(r'abc'
,'abccabc'
(repeat1)
#abcc
重複 「abc」 1次到5次均可,此處為3次repeat2=re.search(r'(abc)'
,'abcabcabc'
(repeat2)
#abcabcabc
(2) " ? "
重複0次或1次
repeat5=re.search(r'(abc)?'
,'abcabcabcabc'
(repeat5)
#abc
(3)" * "repeat6=re.search(r'ab(def)?'
,'abc'
(repeat6)
#ab
重複0次到若干次
repeat3=re.search(r'(abc)*'
,'abcabc'
(repeat3)
#abcabc
(4)" + "repeat4=re.search(r'abc(ba)*'
,'abcabc'
(repeat4)
#abc
重複1次到若干次
貪婪(1) 貪婪模式
" <.+> " == 尖括號內任意內容,重複1到若干次greedy=re.search(r'<.+>'
,'123'
(greedy)
#123
.+ == html>
123(2)非貪婪模式 " <.+?> "
" .+ "不能打括號nogreedy1=re.search(r'<.+?>'
,'123'
(nogreedy1)
#
nogreedy2=re.search(r'(<.+?>)*',''
(nogreedy2)
#nogreedy3=re.search(r'(<.+?>)?',''
(nogreedy3)
#
正規表示式 元字元
現在你已經知道幾個很有用的元字元了,如 b 還有 d 當然還有更多的元字元可用,比如 s 匹配任意的空白符,包括空格,製表符 tab 換行符,中文全形空格等 w匹配字母或數字或下劃線或漢字等。ba w b 匹配以字母a 開頭的單詞 先是某個單詞開始處 b 然後是字母a 然後是任意數量的字母或數字 w...
正規表示式 元字元
元字元 描述.點 匹配任何單個字元。例如正規表示式r.t匹配這些字串 rat rut r t,但是不匹配root。匹配行結束符。例如正規表示式weasel 能夠匹配字串 he s a weasel 的末尾 但是不能匹配字串 they are a bunch of weasels.匹配一行的開始。例如...
正規表示式元字元
l 基本元字元 元字元說明 匹配任意單個字元 邏輯或操作符 定義乙個字元集合,匹配該集合中的乙個字元 對字元集合求非 是對整個集合求非,而不是緊挨著 符號的字元 在字元集合中定義乙個區間。如 a za z 對下乙個字元轉義。比如 n表示換行。數量元字元 元字元說明 匹配前乙個字元 子表示式 零次或多...