re模块

  • re模块中封装了很多正则表达式相关的函数,非常方便的使用正则表达式对字符串进行各种规则匹配检查,常用一个函数是match函数
    • match(patten,string)函数:用于对字符串进行正则匹配,patten表示正则表达式,string表示待匹配字符串,匹配成功返回Match对象,否则返回None
      ```python

      match 函数 (从左开始匹配,没匹配成功,不会向后面匹配,匹配失败)

      import re str1 = “hello python” str2 = “python hello python” rs1 = re.match(“python”,str1) rs2 = re.match(“python”,str2) print(rs1) # None print(rs2) #

取值

print(rs2.group()) #python

  1. <a name="bGN6c"></a>
  2. ### 单字符匹配
  3. - 常用单字符匹配符号:
  4. | 符号 | 说明 |
  5. | --- | --- |
  6. | . | 匹配除`\\n`<br />之外的任意字符 |
  7. | \\d | 匹配0到9之间的一个数字,等价于`[0~9]` |
  8. | \\D | 匹配一个非数字字符,等价于`[^0~9]` |
  9. | \\s | 匹配任意空白字符,如:空格、制表符`\\t`<br />、换行`\\n`<br />等 |
  10. | \\S | 匹配任意非空白字符 |
  11. | \\w | 匹配单词字符,包括字母、数字、下划线 |
  12. | \\W | 匹配非单词字符 |
  13. | [] | 匹配[]中列举的字符 |
  14. ```python
  15. # 单字符匹配
  16. import re
  17. rs = re.match('.','1') # 匹配一个包含数字的字符串
  18. print(rs.group())
  19. rs = re.match('.','a') # 匹配一个包含单字符的字符串
  20. print(rs.group())
  21. rs = re.match('.','abc') # 匹配一个包含多字符的字符串
  22. print(rs.group())
  23. # 多字符匹配
  24. rs = re.match('...','abc') # 匹配一个包含多字符的字符串
  25. print(rs.group())
  26. #匹配任意空白字符
  27. rs = re.match("\s","\t")
  28. print(rs)
  29. rs = re.match("\s","\n")
  30. print(rs)
  31. rs = re.match("\s"," ")
  32. print(rs)
  33. # 匹配[]中的字符
  34. rs = re.match("[hl]","hello")
  35. print(rs)
  36. rs = re.match("[Hh]","hello")
  37. print(rs)
  38. rs = re.match("[0123456789]","2")
  39. print(rs)
  40. rs = re.match("[0-9]","2")
  41. print(rs)

数量表示

  • 常用的数量表示字符:
    | 符号 | 说明 | | —- | —- | | * | 匹配一个字符出现0次或者多次 | | + | 匹配一个字符至少出现一次,等价于{1,} | | ? | 匹配一个字符至多出现一次,就是出现0次或者1次,等价于{0,1} | | {m} | 匹配一个字符出现m次 | | {m,} | 匹配一个字符至少出现m次 | | {m,n} | 匹配一个字符出现m到n次 |
  1. # 匹配字符是否格式一样
  2. str1 ='zhangsan,180,18'
  3. str2 ='lisi,,20'
  4. rs1 = re.match('\w+,[0-9]{3},\d+',str1)
  5. rs2 = re.match('\w+,[0-9]{3},\d+',str2)
  6. if rs1 != None:
  7. print(rs1.group())
  8. else:
  9. print(rs1)
  10. if rs2 != None:
  11. print(rs2.group())
  12. else:
  13. print(rs2)

边界表示

  • 边界表示符号:
    | 符号 | 说明 | | —- | —- | | ^ | 匹配字符串开头 | | $ | 匹配字符串结尾 |
  1. # 边界
  2. phone = '13456788765'
  3. rs = re.match('1[3456789]\d{9}$',phone) #结束边界
  4. print(rs)
  5. phone = 'a13456788765'
  6. rs = re.match('^[a-z]\d*',phone) #开始边界
  7. if rs != None:
  8. print("手机号只能以数字开头")
  9. else:
  10. print(rs.group())

转义字符

  • 转义字符是将一些具有特殊功能的字符转换成普通字符,Python中的原生字符串可以解决多个斜杠问题,
    原生字符串的表示方式就是在字符串前加一个字母r,例如:str=r'abc\\def'
    ```python

    转义字符 (ip匹配)

    str1 = ‘123.123.123.123’ rs1 = re.match(‘\w{3}.\w{3}.\w{3}.\w{3}’,str1) print(rs1) #

    使用q替代.

    str2 = ‘123q123q123q123’ rs2 = re.match(‘\w{3}.\w{3}.\w{3}.\w{3}’,str2) print(rs2) #匹配的结果有问题

    .使用转义字符

    str2 = ‘123q123q123q123’ rs2 = re.match(‘\w{3}.\w{3}.\w{3}.\w{3}’,str2) print(rs2) # None

使用原生字符串

str = “qwer\qwer” print(str) #qwer\qwer 没使用打印结果

str = r”qwer\qwer” print(str) #qwer\qwer 使用打印结果

  1. <br />
  2. <a name="U8XDj"></a>
  3. ### 匹配分组
  4. - 常用的分组符号:
  5. | 符号 | 说明 |
  6. | --- | --- |
  7. | &#124; | 连接多个表达式,表达式之间是`或`<br />的关系,匹配`&#124;`<br />连接的任何表达式 |
  8. | () | 将括号中字符作为一个分组 |
  9. | `\\NUM` | 结合`()`<br />分组使用,引用分组NUM(表示分组编号)对应的匹配规则 |
  10. | `(?P<name>)` | 结合分组起别名 |
  11. | `(?P=name)` | 根据组名使用分组中的正则表达式 |
  12. ```python
  13. # 匹配QQ,163的邮箱
  14. rs1 =re.match(r"\w{4,10}@(163|qq)\.com$","qwer123@163.com")
  15. rs2 =re.match(r"\w{4,10}@(163|qq)\.com$","123456@qq.com")
  16. print(rs1) # <re.Match object; span=(0, 15), match='qwer123@163.com'>
  17. print(rs2) # <re.Match object; span=(0, 13), match='123456@qq.com'>
  18. # 分组匹配网页标签
  19. html_str1 ='<head><title>python</head></title>'
  20. rs1= re.match(r'<(.+)><(.+)>.+</\2></\1>',html_str1)
  21. print(rs1) #错误标签 打印结果None
  22. html_str2 ='<head><title>python</title></head>'
  23. rs2= re.match(r'<(.+)><(.+)>.+</\2></\1>',html_str2)
  24. print(rs2) #正确 <re.Match object; span=(0, 34), match='<head><title>python</title></head>'>

内置函数

  • 合理利用Python的内置函数,可以简化开发,提高开发
    • compile函数:编译正则表达式,返回一个正则表达式对象,可以多次重复使用
      ```python

      compile函数

      pattern =re.compile(“\w{4,10}@163.com$”) #可复用的正则表达式对象 rs1 =re.match(pattern,’123456@163.com’) print(rs1) #

rs2 =re.match(pattern,’qwe@163.com’) print(rs2) # None

rs3 =re.match(pattern,’zhangsan@163.com’) print(rs3) #

  1. - ** search函数**:从左到右在字符串的任意位子搜索第一个被正则表达式匹配的子字符串
  2. ```python
  3. # search函数
  4. rs = re.search("python","hello python ! ni hao ya")
  5. print(rs) # <re.Match object; span=(6, 12), match='python'>
  • findall函数:在字符串中查找正则表达式匹配成功的所有子字符串,返回一个列表

    1. # findall函数
    2. str ='zhangsan:18012345432,lisi:19012345432'
    3. list= re.findall(r"1[35789]\d{9}",str)
    4. print(list) # ['18012345432', '19012345432']
  • finditer函数:在字符串中查找正则表达式匹配成功的所有子字符串,返回一个可迭代对象Iterator

    1. # finditer函数
    2. str ='zhangsan:18012345432,lisi:19012345432'
    3. objs= re.finditer(r"1[35789]\d{9}",str)
    4. for obj in objs:
    5. print(obj.group())
  • sub函数:将正则表达式匹配到的子字符串使用新的字符串替换掉,返回结果是替换后的新字符串,原字符串不变

    1. # sub函数
    2. str ='zhangsan:18012345432:20'
    3. new_str=re.sub(":",",",str)
    4. print(str)
    5. print(new_str)

非贪婪与贪婪模式

  • 贪婪模式:尽可能多地匹配字符

    1. # 贪婪模式
    2. rs1 =re.findall("python\d*","python2020") #任意多个数字
    3. rs2 =re.findall("python\d+","python2020") #至少出现一个数字
    4. rs3 =re.findall("python\d{2,}","python2020") # 至少出现两次数字
    5. rs4 =re.findall("python\d{1,4}","python2020") # 出现1到4次数字
    6. print(rs1) #['python2020']
    7. print(rs2) #['python2020']
    8. print(rs3) #['python2020']
    9. print(rs4) #['python2020']
  • 非贪婪模式:尽可能少地匹配字符串,启用方式是在表示数量的符号后面添加一个问号

    1. # 非贪婪模式
    2. rs1 =re.findall("python\d*?","python2020")
    3. rs2 =re.findall("python\d+?","python2020")
    4. rs3 =re.findall("python\d{2,}?","python2020")
    5. rs4 =re.findall("python\d{1,4}?","python2020")
    6. print(rs1) #['python']
    7. print(rs2) #['python2']
    8. print(rs3) #['python20']
    9. print(rs4) #['python2']

  • 当才华还配不上野心,就静下来学习