Python3---标准库---re

前言

该文章主要描述了re模块的使用

创建时间:20191223

天象独行

  正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。

  注意:如果对正则表达式不了解可以查看连接:

  参考文件1:https://www.cnblogs.com/peng104/p/9619801.html#autoid-1-0-0

  参考文件2:https://www.cnblogs.com/hello-wei/p/10181055.html

  0X01;re.match(pattern,string,flags=0)函数主要功能是从字符串起始位置匹配一个模式,如果不是在起始位置匹配成功,则返回None。

参数   描述
pattern 匹配的正则表达式
string 要匹配的字符串
flags 标志位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。

举例:

import re
print("----------------------------------------------------")
new_str1 = "Aaron is a genius."
new_str2 = "genius is my Aaron."
print("Aaron在起始位置:",re.match("Aaron",new_str1))
print("Aaron不在起始位置:",re.match("Aaron",new_str2))
print("获取匹配字段位置:",re.match("Aaron",new_str1).span())
print("----------------------------------------------------")

结果:

C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/.PyCharmCE2019.3/config/scratches/scratch.py
----------------------------------------------------
Aaron在起始位置: <re.Match object; span=(0, 5), match='Aaron'>
Aaron不在起始位置: None
获取匹配字段位置: (0, 5)
----------------------------------------------------

Process finished with exit code 0

  0X02;re.search(pattern,string,flags=0)作用扫描整个字符串并返回一个匹配的对象,否则返回None

flags 标志位,用于控制正则表达式的匹配方式
pattern 匹配的正则表达式
string   要匹配的字符串

举例:

1 import re
2 
3 new_str1 = "Aaron is a genius, genius is my Aaron."
4 print("匹配字符串:",re.search("Aaron",new_str1))

结果:

1 C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/.PyCharmCE2019.3/config/scratches/scratch.py
2 匹配字符串: <re.Match object; span=(0, 5), match='Aaron'>
3 
4 Process finished with exit code 0

  re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。

  0X03;re.sub,re.subn用于替换字符串中的匹配项。(re.subn与re.sub区别在于re.subn会返回替换的次数)

  re.sub(pattern,repl,string,count=0,flags=0)

  • pattern : 正则中的模式字符串。
  • repl : 替换的字符串,也可为一个函数。
  • string : 要被查找替换的原始字符串。
  • count : 模式匹配后替换的最大次数,默认 0 表示替换所有的匹配。
import re
new_str = "Aaron is a genius, genius is a Aaron."
#全部替换
new_str2 = re.sub("Aaron","Peter",new_str)
new_str3 = re.subn("Aaron","Peter",new_str)
#替换一次
new_str4 = re.sub("Aaron","Peter",new_str,count=1)
new_str5 = re.subn("Aaron","Peter",new_str,count=1)
print(new_str2)
print(new_str3)
print(new_str4)
print(new_str5)

结果:

C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/.PyCharmCE2019.3/config/scratches/scratch.py
Peter is a genius, genius is a Peter.
('Peter is a genius, genius is a Peter.', 2)
Peter is a genius, genius is a Aaron.
('Peter is a genius, genius is a Aaron.', 1)

Process finished with exit code 0

  0X04;re.findall()匹配全部关键字

举例:

import re
new_str = "Aaron is a genius, genius is a Aaron."
print(re.findall("Aaron",new_str))

结果:

C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/.PyCharmCE2019.3/config/scratches/scratch.py
['Aaron', 'Aaron']

Process finished with exit code 0

  0X05;re.compile(pattern[,flags])作用编译正则表达式,生成一个正则表达式(Pattern)对象,供match()和search()这两个函数的使用。

  • pattern : 一个字符串形式的正则表达式

  • flags : 可选,表示匹配模式,比如忽略大小写,多行模式等,具体参数为:

    1. re.I 忽略大小写
    2. re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境
    3. re.M 多行模式
    4. re.S 即为 . 并且包括换行符在内的任意字符(. 不包括换行符)
    5. re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库
    6. re.X 为了增加可读性,忽略空格和 # 后面的注释
import re
pattern = re.compile("Aaron") #编译正则表达式为Pattern
new_str = "Aaron is a genius, genius is my Aaron."
m = pattern.match(new_str)
print(m)

结果:

C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.exe C:/Users/aaron/.PyCharmCE2019.3/config/scratches/scratch.py
<re.Match object; span=(0, 5), match='Aaron'>

Process finished with exit code 0
posted @ 2019-12-30 15:07  天象独行  阅读(237)  评论(0编辑  收藏  举报