#普通字元
import re
result = re.findall(
"p",
"python"
)print
(result)
result = re.findall(
"python"
,"i like python"
)print
(result)
#第一:預定義字元
#\d 匹配所有數字等同於0——9
result = re.findall(r"\d"
,"123456789abcdf"
)print
(result)
#\d 匹配所有的非數字 ^\d
result = re.findall(r"\d"
,"123456789abcdf"
)print
(result)
#\w 包括下劃線字元 a-z a-z 0-9 和下劃線
result = re.findall(r"\w"
,"123456789_abcdf$%&"
)print
(result)
result = re.findall(r"\w"
,"123456789_abcdf$%&"
)print
(result)
result = re.findall(r"\s"
,"123 456 7\t89_abc\ndf$%&"
)print
(result)
#\s 取反
result = re.findall(r"\s"
,"123456789_abcdf$%&"
)print
(result)
#元字元
result = re.findall(r"[123]"
,"123 456789abcdf$%&"
)print
(result)
result = re.findall(r"[12ab]"
,"123 456\n78\t9abcdf$%&"
)print
(result)
result = re.findall(r"[\d\s]"
,"123 456\n78\t9abcdf$%&"
)print
(result)
result = re.findall(r"[^\d\s]"
,"123 456\n78\t9abcdf$%&"
)print
(result)
result = re.findall(r"[1-7]"
,"123 456\n78\t9abcdf$%&"
)print
(result)
result = re.findall(r"[a-d]"
,"123 456\n78\t9abcdf$%&"
)print
(result)
result = re.findall(r"[1-7a-d_%&\s]"
,"123 456\n78\t9ab_cdf$%&"
)print
(result)
result = re.findall(r"a[abc]"
,"aaabacad"
)print
(result)
result = re.findall(r"a([abc])"
,"aaabacad"
)print
(result)
# result = re.findall(r"\d\d\d" ,"123456789abcdf")
# print(result)
result = re.findall(r"\d"
,"123456789abcdf"
)print
(result)
result = re.findall(r"\d"
,"123a45678b90abcdf"
)print
(result)
result = re.findall(r"\d"
,"1234567890abcdf"
)#預設貪婪模式匹配
print
(result)
result = re.findall(r"\d?"
,"1234567890abcdf"
)#非貪婪
print
(result)
result = re.findall(r"\d"
,"12a34567890b1666666661abcdf"
)#預設貪婪模式匹配
print
(result)
result = re.findall(r"\d?"
,"1234567890abcdf"
)print
(result)
result = re.findall(r"\w?"
,"1234567890abcdf"
)print
(result)
result = re.findall(r"\d+?"
,"1234567890abcdf"
)print
(result)
result = re.findall(r"\d+"
,"1234567890abcdf"
)print
(result)
result = re.findall(r"\d*"
,"1234567890abcdf"
)print
(result)
result = re.findall(r"\*"
,"1234*56789*0abcdf"
)print
(result)
result = re.findall(r"4\*5"
,"1234*56789*0abcdf"
)print
(result)
result = re.findall(r"4*5"
,"1234*567859*0abcdf"
)print
(result)
result = re.findall(r"d\w+d"
,"d******d***xxd"
)print
(result)
result = re.findall(r"d\w+?d"
,"d******d***xxd"
)print
(result)
htmlstr =
"python
$123
result = re.findall(r".+"
,htmlstr)
print
(result)
result = re.findall(r".+?/td>"
,htmlstr)
print
(result)
result = re.findall(r"(.+?)"
,htmlstr)
print
(result)
#反向引用
wordstr =
"""'hello' "python" 'love" "haha'
"""result = re.findall(r"['\"]\w+['\"]"
,wordstr)
print
(result)
result = re.findall(r"('|\")(\w+)(\1)"
,wordstr)
print
(result)
print
([x[1]
for x in result]
)#校驗密碼 122223 111222 121212 123321
result = re.findall(r"(\d)(\1)"
,"111222"
)print
(result)
print
([x[1]
+x[0
]for x in result]
)
result = re.findall(r"\d"
,"137000000001232312"
)print
(result)
result = re.findall(r"^\d"
,"137000000001232312"
)print
(result)
result = re.findall(r"^\d$"
,"137000000001232312"
)#從開頭到結尾一共11個
print
(result)
result = re.findall(r"^\d$"
,"13700000000"
)print
(result)
正規表示式知識
字元描述 將下乙個字元標記為乙個特殊字元 或乙個原義字元 或乙個 向後引用 或乙個八進位制轉義符。例如,n 匹配字元 n n 匹配乙個換行符。序列 匹配 而 則匹配 匹配輸入字串的開始位置。如果設定了regexp物件的multiline屬性,也匹配 n 或 r 之後的位置。匹配輸入字串的結束位置。如...
基本正規表示式
匹配除了換行符以外的任意字元 w 匹配字母或數字或下劃線或漢字 字母大寫即反義 不匹配 s 匹配任意的空白符 字母大寫即反義 不匹配 d 匹配數字 字母大寫即反義 不匹配 b 匹配單詞的開始或結束 字母大寫即反義 不匹配 匹配字串的開始 匹配字串的結束 x 匹配除了x以外的任意字元 aeiou 匹配...
正規表示式 基本
字元 d任意乙個數字,0 9中任意乙個 d任意非數字 w任意乙個字母 下劃線 數字。即 a z a z 0 9 w表示除 0 9a za z 之外的字元 s匹配任何空白字元,包括空格 製表符 換頁符等等。等價於 f n r t v s匹配任何非空白字元。匹配任意乙個字元 如要匹配包括 n在內的所有字...