Python 正则表达式(regex)
Python 正则表达式(regex)
正则表达式
正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑
正则表达式非Python独有,在Python中使用re模块实现
常见匹配模式
模式 描述 \w 匹配数字、字母、下划线 \W 匹配非数字、字母、下划线 \s 匹配任意空白字符,等价于[\t\n\r\f] \S 匹配任意非空字符 \d 匹配任意数字,等价于[0-9] \D 匹配任意非数字 \A 匹配字符串开始 \Z 匹配字符串结束,如果是存在换行,只匹配到换行前的结束字符串 \z 匹配字符串结束 \G 匹配最后匹配完成的位置 \n 匹配一个换行符 \t 匹配一个制表符 ^ 匹配字符串的开头 $ 匹配字符串的末尾 . 匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符。 [...] 用来表示一组字符,单独列出:[abc]匹配"a","b"或"c" [^...] 不再[]中的字符:[^abc]匹配除了a,b,c之外的字符 * 匹配0个或多个的表达式 + 匹配1个或多个的表达式 ? 匹配0个或1个由前面的正则表达式定义的片段,非贪婪模式 {n} 精确匹配n个前面表达式 {n,m} 匹配n到m次由前面的正则表达式定义的片段,贪婪模式 a|b 匹配a或b () 匹配括号内的表达式,也表示一个组
re.match
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话, match()就返回none
re.match(pattern,string,flags=0)
常规匹配
import re content = 'Hello 111 2222 World hello python' print(len(content)) res = re.match('^Hello\s\d\d\d\s\d{4}\s\w{5}\s.*python$', content) print(res) print(res.group()) print(res.span())
运行结果: 33 <_sre.SRE_Match object; span=(0, 21), match='Hello 111 2222 World '> Hello 111 2222 World (0, 21)
泛匹配
import re content = 'Hello 111 2222 World hello python' res = re.match('^Hello.*python$', content) print(res) print(res.group()) print(res.span())
运行结果: <_sre.SRE_Match object; span=(0, 33), match='Hello 111 2222 World hello python'> Hello 111 2222 World hello python (0, 33)
匹配目标
import re content = 'Hello 111 2222 World hello python' res = re.match('^Hello\s(\d+)\s(\d+)\s.*python$', content) print(res) print(res.group(1), res.group(2))
运行结果: <_sre.SRE_Match object; span=(0, 33), match='Hello 111 2222 World hello python'> 111 2222
贪婪模式
import re content = 'Hello 111 2222 World hello python' res = re.match('^H.*(\d+)\s(\d+).*python$', content) print(res) print(res.group(1), res.group(2))
运行结果: <_sre.SRE_Match object; span=(0, 33), match='Hello 111 2222 World hello python'> 1 2222
非贪婪模式
import re content = 'Hello 111222 World hello python' res = re.match('^He.*?(\d+).*?python$', content) print(res) print(res.group(1))
运行结果: <_sre.SRE_Match object; span=(0, 31), match='Hello 111222 World hello python'> 111222
匹配模式
模式 描述 re.I 匹配的字符忽略大小写 re.M 多行匹配 re.L 本地化识别匹配 re.U 根据Unicode进行相应化解析 re.S 让 . 匹配包括换行符
import re content = """Hello 1112222 World hello python""" res = re.match('^H.*?(\d+).*?python$', content, re.S) print(res) print(res.group(1))
运行结果: <_sre.SRE_Match object; span=(0, 43), match='Hello 1112222 World \n hello python'> 1112222
转义
import re content = """The apple's price is $5.00""" res = re.match('The apple\'s price is \$5.00', content, re.S) print(res) print(res.group())
<_sre.SRE_Match object; span=(0, 26), match="The apple's price is $5.00"> The apple's price is $5.00
总结:尽量使用泛匹配、使用括号得到匹配目标、尽量使用非贪婪模式、由换行符就用re.S
re.search
re.search 扫描整个字符串并返回第一个成功的匹配
# 使用re.match() import re content = """This is a string""" res = re.match('a', content, re.S) print(res)
运行结果: None
# 使用re.search() import re content = """This is a string""" res = re.search('a\s\w*', content, re.S) print(res) print(res.group())
运行结果: <_sre.SRE_Match object; span=(8, 16), match='a string'> a string
总结:为匹配方便,能用search就不用match
re.findall
搜索字符串,以列表形式返回全部能匹配的子串
import re content = """This is a string""" res = re.findall('a\s\w*', content, re.S) print(res)
运行结果: ['a string']
re.sub
替换字符串中每一个匹配的子串后返回替换后的字符串
import re content = """This is 222211111 string""" res = re.sub('\d+', 'a',content) print(res)
运行结果: This is a string
re.compile
将正则字符串编译成正则表达式对象
将一个正则表达式串编译成正则对象,以便于复用该匹配模式
import re content = """This is 222211111 string""" pattern = re.compile('\d+') res = re.search(pattern, content) print(res) print(res.group())
运行结果: <_sre.SRE_Match object; span=(8, 17), match='222211111'> 222211111
欢迎访问
个人博客地址:www.limiao.tech

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Java并发整理
读Java并发专题总结 一. 基础知识 新建线程 继承Thread类,重写run方法 实现Runable接口 实现Callable接口 线程状态 NEW/Runable/BLOCKED/TIMED _WAITING/WAITING/TERMINATED 调用wait()、join()、LockSupport.lock()方法, 线程会进入到WAITING状态 调用带超时时间的wait(long timeout)、sleep(long)、join(long)、LockSupport.parkNanos()、LockSupport.parkUtil()方法, 线程会进入到TIMED_WAITING状态 调用Object.notify()、Object.notifyAll()方法使线程转换到Runable状态 进入synchronized方法或者synchronized代码块时,线程切换BLOCKED状态 与WAITING状态相关联的是等待队列,与BLOCKED状态相关的是同步队列 线程协作 join()、join(long millis) : 一个线程等待另一个线程结束 sleep()与w...
- 下一篇
Python网络编程 —— IP、UDP
Python网络编程 —— IP、UDP IP ip地址: 在网络中标识一台唯一的设备 ip地址的作用: 通过ip地址在网络中找到对应的设备,然后可以给这个设备发送数据 ip地址分为:ipv4 ipv6 域名:方便记忆某台电脑的主机地址,域名能解析出来一个ip地址(DNS解析) ping 127.0.0.1 能ping通即代表电脑网卡没有问题,网络正常就能上网 网络通信流程: 通过ip地址找到对应的设备(电脑,手机等),然后通过端口号找到端口,再通过端口给应用程序发送数据 端口号分为知名端口号和动态端口号(知名端口号是系统使用的,动态端口号是程序员设置使用的) 知名端口号:范围从0-1023 动态端口范围:1024-65535 ,当程序关闭时,同时也就释放了所占用的端口号 查看端口号:netstat -an 查看端口号被哪个程序占用: lsof -i[tcp/udp]:端口号 (找不到时,使用管理员权限,加sudo) 根据进程编号杀死指定进程:kill -9 进程号 UDP 概念:英文全拼(User Datagram Protocol)简称用户数据报协议,它是无连接的、不可靠的网络传输...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7设置SWAP分区,小内存服务器的救世主
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- Linux系统CentOS6、CentOS7手动修改IP地址
- Docker安装Oracle12C,快速搭建Oracle学习环境
- SpringBoot2全家桶,快速入门学习开发网站教程
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装