python re 正則表達式彙總

import re # text = "1a+_1hello\n" # text1 = '0731-8888888 abc' # ret = re.match('he',text) # ret = re.match('.',text) #.表示可以匹配任意字符 # ret = re.match('\d',text) # 匹配到數字 # ret = re.match('\D',text) # 匹配的任意非數字和\d相反 # ret = re.match('\s',text) #匹配空白字符(\n,\t,\r)以上都是開頭匹配的狀態 # ret = re.match('\w',text) # 匹配a-z A-Z 數字和下劃線 # ret = re.match('\W',text) # 和\w相反的 # ret = re.match('[a1]',text) # [] 組合的方式 匹配1 或是 a # ret = re.match('[\d\-]+',text1) #- 要特殊轉義下 才表示- ;[\d\-]表示匹配到一個字符 是數字或是 - 開頭的,如果是多個的話用+ # ret = re.match('[^0-9]',text1) # 匹配到0-9的數字,如果用一個^表示非 # ret = re.match('[a-zA-Z0-9_]',text1) #這個和\w一樣的效果 # ret = re.match('[^a-zA-Z0-9_]',text1)# 這個是和\W一樣 # print(ret.group()) ##########匹配多個字符: # text = "1a+_1hello\n" # text1 = '0731-8888888 abc' # # ret = re.match('\d*',text1)# * 表示匹配前面0個或是任意多個字符 # ret = re.match('\w+',text1) # + 匹配一個或者多個字符,最少一個 按照全面的字符匹配是字符或是數字 # ret = re.match('\w?',text1) # ?匹配一個或是0個,要麼就匹配一個 # ret = re.match('\w{2}}',text1) # {} 匹配多少個 # ret = re.match('\{m,n}+',text1) # {m,n}匹配m 到 n個字符 # print(ret.group()) ########## # text = '18534523233' # text1 = '[email protected]' # text2 = 'http://baidu.com' # text3 = '42344519900707551x' # 要求1開頭 2位可以是34587 後面9位隨意: # ret = re.match('1[34587]\d{9}',text) 匹配手機號碼 # ret1 = re.match('.+@[a-z0-9]+\.[a-z]+',text1) # 匹配郵箱 # ret2 = re.match('(http|https|ftp)://[^\s]+',text2) # print(len(text3)) # ret3 = re.match('\d{17}[\dxX]',text3) # 匹配shenfenzheng # print(ret3.group()) ################## #脫子豪 ^ 表示以什麼開始 # text3 = '42344519900707551x' # text4 = '[email protected]' # ret3 = re.match('^4',text3) # 匹配shenfen證,match 函數自帶^字號 表示以什麼開始 # ret3 = re.search('^4',text3) # search不自帶,所以^表示已4開頭的 從什麼開始 ## $表示以什麼結尾 # ret3 = re.match('\[email protected]$',text3) ## 默認情況下 匹配多個字母 這個是貪婪模式 ,非貪婪模式就是後面? # ret3 = re.match('\d+?',text3) # 結果是 41042319830707541 # ret3 = re.match('\d+',text3) # 結果是4 安裝最小條件匹配 滿足條件最短的那個 # text = '<h1>標題</h1>' # ret3 = re.match('<.+>', text) ## 結果就是 <h1>標題</h1> 貪婪模式 按照最長的匹配 # ret3 = re.match('<.+?>', text) ## <h1> 這個是非貪婪模式按照最小的匹配 # print(ret3.group()) # # ret4 = re.match('[1-9]\d?$|100$', text) # 1-9]\d?只能匹配前面的兩位10, 所以匹配不到100, 但是$後10結尾 沒有匹配到,然後就匹配了100 #######################轉義字符 \ , \\ , \\\\ r##########: # test = 'dfasdfsdf is $99 dfd' # ret = re.search('\$\d+',test) # \ 轉義字符, search 是對整個字符串進行search,但是match是從字符串的第一個開始匹配 # print(ret.group()) # \n 整個是轉義字符,如果要去掉轉義字符的話 加一個\ , 變成了 \\n # text = r'\n' , r表示raw 這樣就表示了 \n # # \ 在正則表達式和python都是做轉義的, # text = '\c' # # python: ‘\\n’ = \n # #\\\\n == \\n # #正則中:\n = # # \\n = \n # ret = re.match('\\\\c',test) # 所以要寫4個,麻煩可以用: # ret = re.match(r'\\c',test) # 在python這一次就不會轉義了\\c,但是等到了正則的話 去掉 \ 就變成了 \c 就進行了匹配 # print(ret.group()) #######################re.groups, findall sub, split, compile ############ text = 'the&apple price is $44 and sell $34' # res = re.search('.*(\$\d+).*(\$\d+)',text) # print(res.group()) # print(res.group(1)) # print(res.groups()) # res = re.findall('\$\d+',text) # print(res) ## output: ['$44', '$34'] # # res_replace = re.sub('\$\d+','0', text) # 函數替換 # print(res_replace) # text = 'hello world ni hao' # res_split = re.split(' ',text) # res_split = re.split('',text) # print(res_split) text2 = "the number is 20.50" r = re.compile('\d+\.?\d*') ret = re.search(r,text2) # compile先放到內存 到時調用 ret = re.compile(r""" #起到一個註釋的作用 \d* #小數點前面的數字 \.? # 小數點本身 """,re.VERBOSE) print(ret.group())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章