^:字符串開始位置與匹配規則符合就匹配,不然不匹配python
匹配字符串開頭。在多行模式中匹配每一行的開頭(Python3+已經失效,配合compile使用)正則表達式
import re s = 'abc123456abc' res = re.findall('^ab',s) print(res)
['ab']django
$:字符串結束位置與匹配規則符合就匹配,不然不匹配函數
匹配字符串末尾,在多行模式中匹配每一行的末尾spa
import re s= 'abc12ab56bc' res = re.findall('bc$',s) print(res)
['bc']code
import re s= 'abc12ab56bc' res = re.findall('[abc]',s) print(res)
['a', 'b', 'c', 'a', 'b', 'b', 'c']對象
import re s= 'abc12ab56bc' res = re.findall('[^ab]',s) print(res)
['c', '1', '2', '5', '6', 'c']get
(.:) 任意字符(除了\n)it
字符後有幾個點就取字符+幾個點的長度
import re s= 'abc12ab56bc' res = re.findall('ab.',s) print(res)
['abc', 'ab5']
import re s= 'abc12ab56bc' res = re.findall('ab...',s) print(res)
['abc1', 'ab56']
import re s= 'abc12ab56bc' res = re.findall('ab...',s) print(res)
['abc12', 'ab56b']
*: 前面的字符0-無窮個
import re s= 'ab2aabbacaaa' res = re.findall('a*',s) print(res)
['a', '', '', 'aa', '', '', 'a', '', 'aaa', '']
+: 前面的字符1-無窮個
import re s= 'ab2aabbacaaa' res = re.findall('a+',s) print(res)
['a', 'aa', 'a', 'aaa']
?: 前面的字符0-1個
import re s= 'ab2aabbacaaa' res = re.findall('a?',s) print(res)
['a', '', '', 'a', 'a', '', '', 'a', '', 'a', 'a', 'a', '']
{m}: 前面的字符m個
import re s= 'ab2aabbacaaa' res = re.findall('a{2}',s) print(res)
['aa', 'aa']
{n,m}: 前面的字符n-m個
import re s= 'ab2aabbacaaa' res = re.findall('a{1,3}',s) print(res)
['a', 'aa', 'a', 'aaa']
預約義字符是在字符集和組裏都是有用的
反斜槓後邊跟普通字符實現特殊功能
import re 1.\d: 數字 s = 's 1 s+\n=$\t2_s 3' print(re.findall('\d', s)) # ['1', '2', '3'] 2.\D: 非數字 print(re.findall('\D', s)) # ['s', ' ', ' ', ' ', ' ', ' ', 's', '+', '\n', '=', '$', '\t', '_', 's', ' ', ' '] 3. \w: 數字/字母/下劃線 print(re.findall('\w', s)) # ['s', '1', 's', '2', '_', 's', '3'] 4.\W: 非數字/字母/下劃線 print(re.findall('\W', s)) [' ', ' ', ' ', ' ', ' ', '+', '\n', '=', '$', '\t', ' ', ' '] 5.\s: 空格/\t/\n print(re.findall('\s', s)) # [' ', ' ', ' ', ' ', ' ', '\n', '\t', ' ', ' '] 6. \S: 非空格/\t/\n print(re.findall('\S', s)) # ['s', '1', 's', '+', '=', '$', '2', '_', 's', '3'] 7.\: 取消意義 s = 'aba\d' print(re.findall(r'a\\d', s)) # ['a\\d'] 8. .*: 貪婪模式(最大化),找到繼續找,讓結果最大化 s = 'abbbcabc' print(re.findall('a.*c', s)) # ['abbbcabc'] 9..*?: 非貪婪模式(最小化),找到就立刻中止 s = 'abbbcabc' print(re.findall('a.*?c', s)) ['abbbc', 'abc'] 10. (): 只要括號內的 s = 'abacad' print(re.findall('a(.)', s)) # 除了a都要,一次只匹配一個字符 # ['b', 'c', 'd'] 11. A|B: A和B都要 s = 'abacad' print(re.findall('a|b', s)) #['a', 'b', 'a', 'a']
s = '123abc123\ndef456' print(re.findall('\d+', s)) # 匹配全部我的數字
['123', '123', '456']
res = re.match('\d+', s) print(res) print(res.group())
<_sre.SRE_Match object; span=(0, 3), match='123'>
123
res = re.search('\d+', s) print(res) print(res.group())
<_sre.SRE_Match object; span=(0, 3), match='123'>
123
s1 = 'abc324asdfk234lkjsf324lkj' print(re.split('\d+', s1))
['abc', 'asdfk', 'lkjsf', 'lkj']
s1 = 'abc324asdfk234lkjsf324lkj' print(re.split('\d+', s1))
abc***asdfk***lkjsf***lkj
s1 = 'abc324asdfk234lkjsf324lkj' print(re.sub('\d+', '***', s1))
('abc***asdfk***lkjsf***lkj', 3)
s = 'abc123edf456' res = re.search('abc(?P<abc>\d+)edf(?P<edf>\d+)', s) print(res.groupdict())
{'abc': '123', 'edf': '456'}
這個方法是Pattern類的工廠方法,用於將字符串形式的正則表達式編譯爲Pattern對象。 第二個參數flag是匹配模式,取值可使用按位或運算符'|'表示同時生效,好比re.I | re.M。另外,你也能夠在regex字符串中指定模式,好比re.compile('pattern', re.I | re.M)與re.compile('(?im)pattern')是等價的。
下表是全部的正則匹配模式:
修飾符 | 描述 |
---|---|
re.I | 使匹配對大小寫不敏感 |
re.L | 作本地化識別(locale-aware)匹配 |
re.M | 多行匹配,影響 ^ 和 $ |
re.S | 使 . 匹配包括換行在內的全部字符 |
re.U | 根據Unicode字符集解析字符。這個標誌影響 \w, \W, \b, \B. |
re.X | 該標誌經過給予你更靈活的格式以便你將正則表達式寫得更易於理解。 |
import re a = '''asdfhellopass: worldaf ''' b = re.findall('hello(.*?)world', a) c = re.findall('hello(.*?)world', a, re.S) print('b is ', b) print('c is ', c) b is [] c is ['pass:\n ']
正則表達式中,「.」的做用是匹配除「\n」之外的任何字符,也就是說,它是在一行中進行匹配。這裏的「行」是以「\n」進行區分的。a字符串有每行的末尾有一個「\n」,不過它不可見。
若是不使用re.S參數,則只在每一行內進行匹配,若是一行沒有,就換下一行從新開始,不會跨行。而使用re.S參數之後,正則表達式會將這個字符串做爲一個總體,將「\n」當作一個普通的字符加入到這個字符串中,在總體中進行匹配。
res = re.findall(r"A", "abc", re.I) print(res) ['a']
s = '12 34/n56 78/n90' re.findall(r'^/d+', s, re.M) # 匹配位於行首的數字 # ['12', '56', '90'] re.findall(r'/A/d+', s, re.M) # 匹配位於字符串開頭的數字 # ['12'] re.findall(r'/d+$', s, re.M) # 匹配位於行尾的數字 # ['34', '78', '90'] re.findall(r'/d+/Z', s, re.M) # 匹配位於字符串尾的數字 # ['90']
# 要求結果:['12', '23', '34'] l = ['1 2 ', '2 3', ' 3 4'] import re print(eval(re.sub(r'\s*', '', str(l)))) ['12', '23', '34']