正則表達式的常用匹配規則
1、匹配某個字符串
text = 'hellohello'
ret = re.match('he', text)
print(ret)
print(ret.group())
2、點(.)匹配任意的字符
text = 'hellohello'
ret = re.match('.', text)
print(ret.group())
3、但點號不能匹配換行符 \n
text = '\na'
ret = re.match('.', text)
print(ret.group())
4、\d 匹配任意的數字
text = '121311'
ret = re.match('\d', text)
print(ret.group())
5、\D 匹配任意的非數字
text = '+111'
ret = re.match('\D', text)
print(ret.group())
6、 \s 匹配空白字符串,\n \t \r
text = '\r1\n'
ret = re.match('\s', text)
print(ret.group())
7、 \w 匹配小寫的a~z, A~Z, 數字和下劃線
text = '__'
ret = re.match('\w', text)
print(ret.group())
8、 \W 和 \w匹配的相反
text = '----'
ret = re.match('\W', text)
print(ret.group())
9、[]只要滿足中括號中的字符,就可以匹配.只要滿足中括號中的一項既可
以匹配
text = '11aaa11'
ret = re.match('[1]', text)
print(ret.group())
text = '0371-119111'
# + 代表可以匹配一個或多個滿足前邊條件的
ret = re.match('[\d\-]+', text)
print(ret.group())
10、 中括號的形式代替\d
text = '11aaa11'
ret = re.match('[0-9]', text)
print(ret.group())
# \D == [^0-9]
# \w == [0-9a-zA-Z_]
# \W == [^0-9a-zA-Z_]
匹配多個字符串
1、* 可以匹配 0或者任意多個字符
text = '1aaaaa11'
ret = re.match('\d*', text)
print(ret.group())
#如果這裏text = ‘aaa’
那麼匹配0個,但不會報錯
2、+ :匹配一個或者跟多個字符
text = 'aaaa11'
ret = re.match('\w+', text)
print(ret.group())
結果:aaaa11
3、?:匹配一個或者0個
text = 'aaaa11'
ret = re.match('\w*', text)
print(ret.group())
4、 {m}: 匹配m個字符
text = 'aaaa11'
ret = re.match('\w{2}', text)
print(ret.group())
結果: aa
5、{m,n}:匹配m到n個,如果有n個滿足,就匹配n個。
若匹配地小於m,則會報錯
text = 'a
aaa11'
ret = re.match('\w{1,3}', text)
print(ret.group())
正則表達式的常用匹配規則
1、^(脫字號):表示以…開始
text = 'hello'
ret = re.search('^h', text)
print(ret.group())
如果^在 中括號中,則表示取反操作
2、$ 代表以…結尾
text = '[email protected]'
ret = re.match('\[email protected]$', text)
print(ret.group())
3、| 匹配多個表達式或者字符串
text = 'https://[email protected]'
ret = re.match('(https|http)://\[email protected]$', text)
print(ret.group())
4、貪婪模式和非貪婪模式
貪婪模式:匹配儘量多的字符 如 +
text = '013212313123123'
ret = re.match('\d+', text)
print(ret.group())
text = '<h1>測試</h1>'
ret = re.match('<.+>', text)
print(ret.group())
結果:<h1>測試</h1>
非貪婪模式:+?
text = '013212313123123'
ret = re.match('\d+?', text)
print(ret.group())
text = '<h1>測試</h1>'
ret = re.match('<.+?>', text)
print(ret.group())
結果
正則表達式的一些使用案例
1、驗證手機號
text = '18511237719'
ret = re.match('1[34578]\d{9}', text)
print(ret.group())
2、驗證郵箱
text = '[email protected]'
ret = re.match('\w+@[a-z0-9]+\.[a-z]{2,3}', text)
print(ret.group())
3、驗證url
text = 'https://mp.csdn.net/mdeditor/103151639#'
ret = re.match('(http|https|ftp)://[^\s]+', text)
print(ret.group())
4、驗證身份證
text = '11012345678965432x'
ret = re.match('\d{17}[\dxX]', text)
print(ret.group()
re模塊常用函數
1、find_all
text = "apple's price $99,orange's price is $10"
ret = re.findall('\$\d+', text)
print(ret)
結果:['$99', '$10']
2、sub
text = "apple's price $99,orange's price is $10"
ret = re.sub('\$\d+', '0', text)
print(ret)
結果:apple's price 0,orange's price is 0
3、split
text = 'hello world ni hao'
ret = re.split(' ', text)
print(ret)
結果:['hello', 'world', 'ni', 'hao']
4、compile
對於一些倡常用的正則表達式,可以使用compile進行編譯,後期再使用時可以直接拿過來使用,使用效率會更高。而且compile可以指定flag=re.VERBOSE,再寫正則表達式時候可以做好註釋
text = 'the number is 20.52'
# r = re.compile('\d+\.?\d*')
r = re.compile(r"""
\d+ #小數點前邊的數字
\.? #小數點本身
\d* #小數點後邊的數字
""", re.VERBOSE)