【Python基础】Python重要的-re模块

Python重要的-re模块

re模块(* * * * *)

就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹配引擎执行。

字符匹配(普通字符,元字符):

1、普通字符:大多数字符和字母都会和自身匹配
              >>> re.findall('alvin','yuanaleSxalexwupeiqi')
                      ['alvin'] 

2、元字符: .  ^  $  *  +  ?  { }  [ ]  |  ( )  \

元字符之 . ^ $ * + ? { }

import re
 
ret=re.findall('a..in','helloalvin')
print(ret)#['alvin']
 
ret=re.findall('^a...n','alvinhelloawwwn')
print(ret)#['alvin']
 
ret=re.findall('a...n$','alvinhelloawwwn')
print(ret)#['awwwn']
 
ret=re.findall('a...n$','alvinhelloawwwn')
print(ret)#['awwwn']
 
ret=re.findall('abc*','abcccc')#贪婪匹配[0,+oo]  
print(ret)#['abcccc']
 
ret=re.findall('abc+','abccc')#[1,+oo]
print(ret)#['abccc']
 
ret=re.findall('abc?','abccc')#[0,1]
print(ret)#['abc']
 
ret=re.findall('abc{1,4}','abccc')
print(ret)#['abccc'] 贪婪匹配

注意:前面的*,+,?等都是贪婪匹配,也就是尽可能匹配,后面加?号使其变成惰性匹配

ret=re.findall('abc*?','abcccccc')
print(ret)#['ab']

跟着教程敲代码练习:

""" 第一类元字符 . ^ $ * + ? {}"""
import re

""" . 匹配除了 \n 之外的任意字符,一个 . 只能匹配一个字符 """
re.findall("alex","djkalfjioealexieou")        ##['alex']

re.findall("a..x",'jowutojoauixuoero')      #['auix']

re.findall("a...x",'jowutojoauixuoero')      #[]

""" ^ 开头匹配 """
re.findall("^a...x",'jowutojoauixuoero')      #[]

re.findall("^j..u",'jowutojowuixuoero')      #['jowu']

re.findall("j..u",'jowutojowuixuoero')  #['jowu', 'jowu']

""" $ 结尾匹配 """
re.findall("j..u$",'jowutojowuixuoejrou')       #['jrou']

re.findall("j..u$",'jowutojowuixuoejrou$')       #[]

""" * 紧挨着的字符 匹配0~无穷次 """
""" + 紧挨着的字符 匹配1~无穷次 """
re.findall("alex*","uoghkalexxx")   #['alexxx']
re.findall("alex+","uoghkalexxx")   #['alexxx']
re.findall("alex*","uoghkale")  #['ale']
re.findall("alex+","uoghkale")  #[]

""" ? 紧挨着的字符 匹配0~1 """
re.findall("alex?","uoghkale")      #['ale']

re.findall("alex?","uoghkalex")     #['alex']

re.findall("alex?","uoghkalexxxx")      #['alex']

""" {} 是上面 * + ? 的集合,并且可自定义
{0,}  <===> *
{1,}  <===> +
{0,1}  <===> ?
{1,6}      1 2 3 4 5 6 都可以
"""
re.findall("alex{6}","uoghkalexxxxxx")      #['alexxxxxx']

re.findall("alex{0,6}","uoghkalexxx")       #['alexxx']

re.findall("alex{0,6}","uoghkale")      #['ale']

re.findall("alex{0,1}","uoghkale")      #['ale']

re.findall("alex{0,1}","uoghkalex")     #['alex']

# 惰性匹配 在元字符后面加上 ?
re.findall("alex*?","uoghkalexxxx")     #['ale']

re.findall("alex+?","uoghkalexxxx")     #['alex']

re.findall("alex+","uoghkalexxxx")      #['alexxxx']
我的理解

元字符之字符集[]:

#--------------------------------------------字符集[]
ret=re.findall('a[bc]d','acd')
print(ret)#['acd']
 
ret=re.findall('[a-z]','acd')
print(ret)#['a', 'c', 'd']
 
ret=re.findall('[.*+]','a.cd+')
print(ret)#['.', '+']
 
#在字符集里有功能的符号: - ^ \
 
ret=re.findall('[1-9]','45dha3')
print(ret)#['4', '5', '3']
 
ret=re.findall('[^ab]','45bdha3')
print(ret)#['4', '5', 'd', 'h', '3']
 
ret=re.findall('[\d]','45bdha3')
print(ret)#['4', '5', '3']

跟着教程敲代码练习:

""" [] 中:^ 非 ,- 表示从。。到。。  a-z 所有小写字母,\ 转义  \(  就表示左括号。
其他的元字符失去意义,如 * 不表示(0-无穷),()就只是表示括号
"""
ret = re.findall('a[bc]d', 'acd')       # 匹配 abd 或 acd
print(ret)  # ['acd']

ret = re.findall('[a-z]', 'acd')   #一个字符 从a到z
print(ret)  # ['a', 'c', 'd']

ret = re.findall('[.*+]', 'a.cd+')  #* 失去特殊意义 就表示一个 *
print(ret)  # ['.', '+']

# 在字符集里有功能的符号: - ^ \

ret = re.findall('[1-9]', '45dha3')
print(ret)  # ['4', '5', '3']

ret = re.findall('[^ab]', '45bdha3')
print(ret)  # ['4', '5', 'd', 'h', '3']

ret = re.findall('[\d]', '45bdha3')
print(ret)  # ['4', '5', '3']
我的理解
# 一个特殊案例:匹配表达式里面 最内层括号(13-6)

re.findall("\([^()]*\)","12+(34*6+2-5*(13-6))")    #['(13-6)']

元字符之转义符 \

反斜杠后边跟元字符去除特殊功能,比如 \.
反斜杠后边跟普通字符实现特殊功能,比如 \d

  • \d  匹配任何十进制数;它相当于类 [0-9]。
  • \D 匹配任何非数字字符;它相当于类 [^0-9]。
  • \s  匹配任何空白字符;它相当于类 [ \t\n\r\f\v]。
  • \S 匹配任何非空白字符;它相当于类 [^ \t\n\r\f\v]。
  • \w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。
  • \W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]
  • \b  匹配一个特殊字符边界,比如空格 ,&,#等

猜你喜欢

转载自www.cnblogs.com/XJT2018/p/10872274.html