python中re模块提供了正则表达式相关操作

字符:

  . 匹配除换行符以外的任意字符
  \w 匹配字母或数字或下划线或汉字
  \s 匹配任意的空白符
  \d 匹配数字
  \b 匹配单词的开始或结束
  ^ 匹配字符串的开始
  $ 匹配字符串的结束

次数:

  * 重复零次或更多次
  + 重复一次或更多次
  ? 重复零次或一次
  {n} 重复n次
  {n,} 重复n次或更多次
  {n,m} 重复n到m次

  1. # match,从起始位置开始匹配,匹配成功返回一个对象,未匹配成功返回None
  2.  
  3. match(pattern, string, flags=0)
  4. # pattern: 正则模型
  5. # string : 要匹配的字符串
  6. # falgs : 匹配模式
  7. X VERBOSE Ignore whitespace and comments for nicer looking RE's.
  8. I IGNORECASE Perform case-insensitive matching.
  9. M MULTILINE "^" matches the beginning of lines (after a newline)
  10. as well as the string.
  11. "$" matches the end of lines (before a newline) as well
  12. as the end of the string.
  13. S DOTALL "." matches any character at all, including the newline.
  14.  
  15. A ASCII For string patterns, make \w, \W, \b, \B, \d, \D
  16. match the corresponding ASCII character categories
  17. (rather than the whole Unicode categories, which is the
  18. default).
  19. For bytes patterns, this flag is the only available
  20. behaviour and needn't be specified.
  21.  
  22. L LOCALE Make \w, \W, \b, \B, dependent on the current locale.
  23. U UNICODE For compatibility only. Ignored for string patterns (it
  24. is the default), and forbidden for bytes patterns.

match

  1. # 无分组
  2. r = re.match("h\w+", origin)
  3. print(r.group()) # 获取匹配到的所有结果
  4. print(r.groups()) # 获取模型中匹配到的分组结果
  5. print(r.groupdict()) # 获取模型中匹配到的分组结果
  6.  
  7. # 有分组
  8.  
  9. # 为何要有分组?提取匹配成功的指定内容(先匹配成功全部正则,再匹配成功的局部内容提取出来)
  10.  
  11. r = re.match("h(\w+).*(?P<name>\d)$", origin)
  12. print(r.group()) # 获取匹配到的所有结果
  13. print(r.groups()) # 获取模型中匹配到的分组结果
  14. print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组

demo

  1. # search,浏览整个字符串去匹配第一个,未匹配成功返回None
  2. # search(pattern, string, flags=0)

search

  1. # 无分组
  2.  
  3. r = re.search("a\w+", origin)
  4. print(r.group()) # 获取匹配到的所有结果
  5. print(r.groups()) # 获取模型中匹配到的分组结果
  6. print(r.groupdict()) # 获取模型中匹配到的分组结果
  7.  
  8. # 有分组
  9.  
  10. r = re.search("a(\w+).*(?P<name>\d)$", origin)
  11. print(r.group()) # 获取匹配到的所有结果
  12. print(r.groups()) # 获取模型中匹配到的分组结果
  13. print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组

demo

  1. # findall,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;
  2. # 空的匹配也会包含在结果中
  3. #findall(pattern, string, flags=0)

findall

  1. # 无分组
  2. r = re.findall("a\w+",origin)
  3. print(r)
  4.  
  5. # 有分组
  6. origin = "hello alex bcd abcd lge acd 19"
  7. r = re.findall("a((\w*)c)(d)", origin)
  8. print(r)

demo

  1. # sub,替换匹配成功的指定位置字符串
  2.  
  3. sub(pattern, repl, string, count=0, flags=0)
  4. # pattern: 正则模型
  5. # repl : 要替换的字符串或可执行对象
  6. # string : 要匹配的字符串
  7. # count : 指定匹配个数
  8. # flags : 匹配模式

sub

  1. # 与分组无关
  2.  
  3. origin = "hello alex bcd alex lge alex acd 19"
  4. r = re.sub("a\w+", "999", origin, 2)
  5. print(r)

demo

  1. # split,根据正则匹配分割字符串
  2.  
  3. split(pattern, string, maxsplit=0, flags=0)
  4. # pattern: 正则模型
  5. # string : 要匹配的字符串
  6. # maxsplit:指定分割个数
  7. # flags : 匹配模式

split

  1. # 无分组
  2. origin = "hello alex bcd alex lge alex acd 19"
  3. r = re.split("alex", origin, 1)
  4. print(r)
  5.  
  6. # 有分组
  7.  
  8. origin = "hello alex bcd alex lge alex acd 19"
  9. r1 = re.split("(alex)", origin, 1)
  10. print(r1)
  11. r2 = re.split("(al(ex))", origin, 1)
  12. print(r2)

demo

  1. IP
  2. ^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$
  3. 手机号:
  4. ^1[3|4|5|8][0-9]\d{8}$
  5. 邮箱:
  6. [a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+

常用正则表达式

【Python】【Module】re的更多相关文章

  1. 【python 字典、json】python字典和Json的相互转换

    [python 字典.json]python字典和Json的相互转换 dump/dumps字典转换成json load/loadsjson转化成字典 dumps.loads直接输出字符 dump.lo ...

  2. 【Python成长之路】Python爬虫 --requests库爬取网站乱码(\xe4\xb8\xb0\xe5\xa)的解决方法【华为云分享】

    [写在前面] 在用requests库对自己的CSDN个人博客(https://blog.csdn.net/yuzipeng)进行爬取时,发现乱码报错(\xe4\xb8\xb0\xe5\xaf\x8c\ ...

  3. 【Python成长之路】装逼的一行代码:快速共享文件

    [Python成长之路]装逼的一行代码:快速共享文件 2019-10-26 15:30:05 华为云 阅读数 335 文章标签: Python编程编程语言程序员Python开发 更多 分类专栏: 技术 ...

  4. 【python之路42】web框架们的具体用法

    Python的WEB框架 (一).Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. p ...

  5. 【Python成长之路】词云图制作

    [写在前面] 以前看到过一些大神制作的词云图 ,觉得效果很有意思.如果有朋友不了解词云图的效果,可以看下面的几张图(图片都是网上找到的): 网上找了找相关的软件,有些软件制作 还要付费.结果前几天在大 ...

  6. 【Python成长之路】装逼的一行代码:快速共享文件【华为云分享】

    [写在前面] 有时候会与同事共享文件,正常人的操作是鼠标右键,点击共享.其实有个装逼的方法,用python的一行代码快速实现基于http服务的共享方式. [效果如下] [示例代码] 在cmd窗口进入想 ...

  7. 【Python成长之路】从 零做网站开发 -- 基于Flask和JQuery,实现表格管理平台

    [写在前面] 你要开发网站?    嗯.. 会Flask吗?    什么东西,没听过... 会JQuery吗?    是python的库吗 ? 那你会什么?    我会F12打开网站 好吧,那我们来写 ...

  8. 朴素贝叶斯算法源码分析及代码实战【python sklearn/spark ML】

    一.简介 贝叶斯定理是关于随机事件A和事件B的条件概率的一个定理.通常在事件A发生的前提下事件B发生的概率,与在事件B发生的前提下事件A发生的概率是不一致的.然而,这两者之间有确定的关系,贝叶斯定理就 ...

  9. 【Python—字典的用法】创建字典的3种方法

    #创建一个空字典 empty_dict = dict() print(empty_dict) #用**kwargs可变参数传入关键字创建字典 a = dict(one=1,two=2,three=3) ...

  10. 【python之路35】网络编程之socket相关

    Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...

随机推荐

  1. CSS 脉冲和火箭动画特效

    CSS脉冲和火箭动画特效 <!DOCTYPE html> <html lang="en"> <head> <meta charset=

  2. hive 权限排查

    show grant role role_username id username

  3. Python基础(list与tuple)

    #list 类似于数组的概念 classmates = ['傻狗1','傻狗2','傻狗3'] # print(classmates) # print(len(classmates)) # print ...

  4. ExtJS 去除水印

    在路径(根目录/ext/classic(或者modern)/theme-base/sass/etc/all.scss)文件中修改\(ext-trial: true !default; 为\)ext-t ...

  5. hover 背后的数学和图形学

    前端开发中,hover是最常见的鼠标操作行为之一,用起来也很方便,CSS直接提供:hover伪类,js可以通过mouseover+mouseout事件模拟,甚至一些第三方库/框架直接提供了 hover ...

  6. dart系列之:创建Library package

    目录 简介 Library package的结构 导入library 条件导入和导出library 添加其他有效的文件 library的文档 发布到pub.dev 总结 简介 在dart系统中,有pu ...

  7. Study Blazor .NET(二)安装

    翻译自:Study Blazor .NET,转载请注明. 安装 请根据下面步骤安装开始使用Blazor: 1.针对不同的操作系统,安装最新版.Net Core框架 [这里] 2.用.Net Core ...

  8. [SQL Server]一列多行转换为字符串

    在 SQL Server 中,如何将多行数据变成一个字符串保存. skill 投石 挖矿 刮痧 上面三行数据想要得到结果为:投石,挖矿,刮痧 有两种方式: 拼接字符串. 使用 for XML 首先创建 ...

  9. 《重学Java高并发》Sempahore的使用场景与常见误区

    大家好,我是威哥,<RocketMQ技术内幕>一书作者,荣获RocketMQ官方社区优秀布道师.CSDN2020博客执之星Top2等荣誉称号.目前担任中通快递技术平台部资深架构师,主要负责 ...

  10. 论文翻译:2020_A Recursive Network with Dynamic Attention for Monaural Speech Enhancement

    论文地址:基于动态注意的递归网络单耳语音增强 论文代码:https://github.com/Andong-Li-speech/DARCN 引用格式:Li, A., Zheng, C., Fan, C ...