python中如何使用正则表达式的集合字符示例
作者:caimouse 时间:2022-05-31 13:00:56
前言
本文主要给大家介绍了关于python使用正则表达式的集合字符的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
在正则表达式里,想匹配一些字符中的一个,也就是说给出一个字符的集合,只要出现这个集合里任意的字符,都是成立的。比如[ab],就是将匹配任意出现a或b的字符。比如a[ab]+,它是贪婪模式,将会匹配所有是a后面的a或b的字符串,如abbaabbba。如果要改为非贪婪模式,要在后面添加?,如下面的例子:
示例代码
#python 3.6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
from re_test_patterns import test_patterns
test_patterns(
'abbaabbba',
[('[ab]', 'either a or b'),
('a[ab]+', 'a followed by 1 or more a or b'),
('a[ab]+?', 'a followed by 1 or more a or b, not greedy')],
)
结果输出如下:
'[ab]' (either a or b)
'abbaabbba'
'a'
.'b'
..'b'
...'a'
....'a'
.....'b'
......'b'
.......'b'
........'a'
'a[ab]+' (a followed by 1 or more a or b)
'abbaabbba'
'abbaabbba'
'a[ab]+?' (a followed by 1 or more a or b, not greedy)
'abbaabbba'
'ab'
...'aa'
来源:http://blog.csdn.net/caimouse/article/details/78173805
标签:python,正则表达式,集合字符
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python编程把二叉树打印成多行代码
2023-06-24 15:12:57
编写Python的web框架中的Model的教程
2022-09-29 22:54:19
解决python路径错误,运行.py文件,找不到路径的问题
2023-03-13 05:47:33
![](https://img.aspxhome.com/file/2023/1/81631_0s.jpg)
Python解析nginx日志文件
2023-09-07 18:22:09
php用header函数实现301跳转代码实例
2023-10-08 11:29:59
Python 实现大整数乘法算法的示例代码
2022-07-07 02:57:54
matplotlib事件处理基础(事件绑定、事件属性)
2023-02-02 19:34:32
Python游戏开发之魔塔小游戏的实现
2022-08-26 16:14:35
![](https://img.aspxhome.com/file/2023/6/66426_0s.gif)
python网络编程之数据传输UDP实例分析
2022-12-07 12:42:41
Python unittest 自动识别并执行测试用例方式
2023-08-14 07:09:39
GoLang 逃逸分析的机制详解
2023-08-06 16:46:43
![](https://img.aspxhome.com/file/2023/8/95358_0s.png)
python网络编程之多线程同时接受和发送
2021-12-01 07:20:09
基于Python实现一个简易的数据管理系统
2023-08-14 11:49:38
![](https://img.aspxhome.com/file/2023/4/92274_0s.png)
Python tkinter padx参数详解
2022-03-12 06:09:50
![](https://img.aspxhome.com/file/2023/7/112537_0s.png)
python标记语句块使用方法总结
2023-09-23 20:16:22
![](https://img.aspxhome.com/file/2023/1/110431_0s.png)
Python 多线程抓取图片效率对比
2021-12-08 10:10:48
浅谈ThinkPHP5.0版本和ThinkPHP3.2版本的区别
2023-09-09 23:41:04
Python格式化输出--%s,%d,%f的代码解析
2022-10-31 06:26:44
![](https://img.aspxhome.com/file/2023/4/90664_0s.png)
python中的json数据和pyecharts模块入门示例教程
2023-02-22 04:58:37
![](https://img.aspxhome.com/file/2023/9/87659_0s.jpg)
简单了解Django ContentType内置组件
2022-08-04 08:57:16
![](https://img.aspxhome.com/file/2023/3/87503_0s.png)