1 ''' 2 常用的正則表達式元字符 3 . 匹配換行符以外的任意單個字符 4 * 匹配位於'*'之前的字符或子模的0次或多次出現 5 + 匹配位於'+'之前的字符或子模式的1次或多次出現 6 - 用在[]之內用來表示范圍 7 | 匹配位於'|'之前或之后的字符 8 ^ 匹配行首,匹配以^后面的字符開頭的字符串 9 $ 匹配行尾,匹配以$之前的字符結束的字符串 10 ? 匹配位於'?'之前的0個或1個字符。當此字符緊隨任何其他限定符(*、+、?、{n}、{n,}、{n,m}之后時,匹配模式是'非貪心的'。'非貪心的'模式匹配 11 盡可能短的字符串,而默認的'貪心的'模式匹配搜索到的、盡可能長的字符串。例如:在字符串'oooo'中,'o+?'只能匹配單個,而'o+'匹配所有o 12 \ 表示位於\之后的為轉義字符 13 \num 此處的num是一個正整數,例如,'(.)\1'匹配兩個連續的相同字符 14 \f 換頁符匹配 15 \n 換行符匹配 16 \r 匹配一個回車符 17 \b 匹配單詞頭或單詞尾 18 \B 與\b含義相反 19 \d 匹配任意數字,相當於[0-9] 20 \D 與\d含義相反,等效於[^0-9] 21 \s 匹配任何空白字符,包括空格、制表符、換頁符,與[\f\n\r\t\v]等效 22 \S 與\s含義相反 23 \w 匹配任何字母、數字以及下划線,相當於[a-zA-Z0-9_] 24 \W 與\w含義相反,與[^a-zA-Z0-9_]等效 25 () 將位於()內的內容作為一個整體來對待 26 {} 按{}中的次數進行匹配 27 [] 匹配位於[]中的任意一個字符 28 [^xyz] ^放在[]表示反向字符集,匹配除x,y,z以外的任何字符 29 [a-z] 字符范圍,匹配指定范圍內的任何字符 30 [^a-z] 反向范圍字符,匹配除小寫英文字母之外的任何字符 31 '''
1 ''' 2 常用子模式擴展語法 3 (? P<groupname>) 為子模式命名 4 (? iLmsux) 設置匹配標志,可以是幾個字母的組合,每個字母含義與編譯標志相同 5 (?:...) 匹配但不捕獲該匹配的子表達式 6 (? P=groupname) 表示在此之前的命名為groupname的子模式 7 (? #...) 表示注釋 8 (? =...) 用於正則表達式之后,表示如果'='后面的內容在字符串中出現則匹配,但不反回'='之后的內容 9 (?! ...) 用於正則表達式之后,表示如果'!'后2的內容在字符串中不出現則匹配,但不返回'='之后的內容 10 (? <=...) 用於正則表達式之前,與(? =...)含義相同 11 (? <!...) 用於正則表達式之前,與(?! ...)含義相同 12 '''
re模塊使用
1 import re 2 ''' 3 compile() 創建模式對象 4 re.search() 在這種整個字符串中尋找模式,返回match對象或None 5 re.match() 從字符串的開始處匹配模式,返回match對象或None 6 re.findall() 列出字符串中模式的所有匹配項 7 re.split() 根據模式匹配項分割字符串 8 re.sub() 將字符串中所有pat的匹配項用repl替換 9 re.escape() 將字符串中所有特殊正則表達式轉義 10 ''' 11 text='alpha.beta...gamma delta' #測試用的字符串 12 print(re.split('[\.]+',text)) 13 print(re.split('[\.]+',text,maxsplit=2)) #最多分隔2次) 14 print(re.split('[\.]+',text,maxsplit=1)) #最多分隔1次 15 pat = '[a-zA-Z]' 16 print(re.findall(pat,text)) #查找所有單詞 17 # ['a', 'l', 'p', 'h', 'a', 'b', 'e', 't', 'a', 'g', 'a', 'm', 'm', 'a', 'd', 'e', 'l', 't', 'a'] 18 pat='{name}' 19 text='Dear {name}...' 20 print(re.sub(pat,'Mr.Dong',text)) #字符串替換 21 # Dear Mr.Dong... 22 s='a s d' 23 print(re.sub('a|s|d','good',s)) 24 # good good good 25 print(re.escape('http://www.python.org')) #字符串轉義 26 # http\:\/\/www\.python\.org 27 print(re.match('done|quit','done')) #匹配成功,返回match對象 28 # <_sre.SRE_Match object; span=(0, 4), match='done'> 29 print(re.match('done|quit','doe!')) #匹配不成功 30 # None 31 print(re.search('done|quit','d!one!done')) #匹配成功 32 # <_sre.SRE_Match object; span=(6, 10), match='done'> 33 34 s='aaa bb c d e fff ' 35 print(' '.join(s.split())) #不使用正則表達式,直接使用字符串對象的方法 36 # aaa bb c d e fff 37 print(re.split('[\s]+',s)) 38 # ['aaa', 'bb', 'c', 'd', 'e', 'fff', ''] 39 print(re.split('[\s]+',s.strip())) #同時使用re模塊中的方法和字符串對象的方法 40 # ['aaa', 'bb', 'c', 'd', 'e', 'fff'] 41 print(' '.join(re.split('[\s]+',s.strip()))) 42 # aaa bb c d e fff 43 print(' '.join(re.split('\s+',s.strip()))) 44 # aaa bb c d e fff 45 print(re.sub('\s+',' ',s.strip())) #直接使用re模塊的字符串替換方法 46 # aaa bb c d e fff 47 48 ''' 49 下面的代碼使用以'\'開頭的元字符來實現字符串的特定搜索 50 ''' 51 example='China institute of business and Technology is a very beautiful school. ' 52 print(re.findall('\\ba.+?\\b',example)) #以字母a開頭的完整單詞,'?'表示非貪心模式 53 # ['and', 'a '] 54 print(re.findall('\\ba.+\\b',example)) #貪心模式的匹配結果 55 # ['and Technology is a very beautiful school'] 56 print(re.findall('\\ba\w*\\b',example)) 57 # ['and', 'a'] 58 print(re.findall('\\Bo.+?\\b',example)) 59 # ['ology', 'ool'] 60 print(re.findall('\\b\w*\\b',example)) #所有單詞 61 # ['China', '', 'institute', '', 'of', '', 'business', '', 'and', '', 'Technology', '', 'is', '', 'a', '', 'very', '', 'beautiful', '', 'school', ''] 62 print(re.findall('\w+',example)) 63 # ['China', 'institute', 'of', 'business', 'and', 'Technology', 'is', 'a', 'very', 'beautiful', 'school'] 64 re.findall(r'\b\w.+?\b',example) #使用原始字符串 65 # ['China', 'institute', 'of', 'business', 'and', 'Technology', 'is', 'a', 'very', 'beautiful', 'school'] 66 print(re.split('\s',example)) #使用任何空白字符分隔字符 67 print(re.findall('\d+\.\d+\.\d+','Python 3.5.988')) #查找並返回x.x.x形式的數字 68 # ['3.5.988'] 69 print(re.findall('\d+\.\d+\.\d+','Python 3.5.988,3.54.45')) 70 71 ''' 72 使用正則表達式對象 73 首先使用re模塊的compile()方法將正則表達式編譯生成正則表達式對象,然后再使用正則表達式對象提供的方法進行字符串處理。使用編譯后的正則表達式對象 74 不僅可以提高字符串處理速度,還提供了更加強大的字符串處理功能。 75 正則表達式對象math(string[,pos[,endpos]])方法用於在字符串開頭或指定位置進搜索,模式必須出現在字符串開頭或指定位置;searc(string[,pos[,endpos]]) 76 方法用於正整個字符串或指定范圍中進行搜索;findall(strinng[,pos[,endpos]])方法用來在整個字符串中查找所有符合正則表達式的字符串並以列表形式返回 77 ''' 78 example='China Institute of Business and Technology' 79 pattern = re.compile(r'\bB\w+\b') #編譯正則表達式對象,查找以B開頭的單詞 80 print(pattern.findall(example)) #使用正則表達式對象的findall()方法 81 # ['Business'] 82 pattern=re.compile(r'\w+y\b') #以字母y結尾的單詞 83 print(pattern.findall(example)) 84 # ['Technology'] 85 pattern = re.compile(r'\b[a-zA-Z]{3}\b') 86 print(pattern.findall(example)) 87 # ['and'] 88 pattern.match(example) #從字符串開頭開始匹配,失敗返回空值 89 print(pattern.search(example)) #在整個字符串中搜索,成功 90 pattern = re.compile(r'\b\w*a\w*\b') #查找所有以字母a的單詞 91 print(pattern.findall(example)) 92 # ['China', 'and'] 93 text ='He was carefully disguised but captured quickly by police.' 94 print(re.findall(r'\w+ly',text)) #查找所有以字母ly結尾的單詞 95 # ['carefully', 'quickly']
