python里使用正则表达式的非贪婪模式
2017-10-07 18:01
579 查看
在正则表达式里,什么是正则表达式的贪婪与非贪婪匹配
如:String str="abcaxc";
Patter p="ab*c";
贪婪匹配:正则表达式一般趋向于最大长度匹配,也就是所谓的贪婪匹配。如上面使用模式p匹配字符串str,结果就是匹配到:abcaxc(ab*c)。
非贪婪匹配:就是匹配到结果就好,就少的匹配字符。如上面使用模式p匹配字符串str,结果就是匹配到:abc(ab*c)。
解决这个问题,可以采用:
正则引擎默认是贪婪的,当出现"*"时,它会尽量去匹配尽可能长的字符串。
一个用于修正以上问题的可能方案是用"*"的惰性代替贪婪性。你可以在"*"后面紧跟一个问号"?"来达到这一点
这告诉正则引擎,尽可能少的重复上一个字符。
如下面的例子:
#python 3. 6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
from re_test_patterns import test_patterns
test_patterns(
'abbaabbba',
[('ab*?', 'a followed by zero or more b'),
('ab+?', 'a followed by one or more b'),
('ab??', 'a followed by zero or one b'),
('ab{3}?', 'a followed by three b'),
('ab{2,3}?', 'a followed by two to three b')],
)
输出结果如下:
'ab*?' (a followed by zero or more b)
'abbaabbba'
'a'
...'a'
....'a'
........'a'
'ab+?' (a followed by one or more b)
'abbaabbba'
'ab'
....'ab'
'ab??' (a followed by zero or one b)
'abbaabbba'
'a'
...'a'
....'a'
........'a'
'ab{3}?' (a followed by three b)
'abbaabbba'
....'abbb'
'ab{2,3}?' (a followed by two to three b)
'abbaabbba'
'abb'
....'abb'
深入浅出Numpy
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
如:String str="abcaxc";
Patter p="ab*c";
贪婪匹配:正则表达式一般趋向于最大长度匹配,也就是所谓的贪婪匹配。如上面使用模式p匹配字符串str,结果就是匹配到:abcaxc(ab*c)。
非贪婪匹配:就是匹配到结果就好,就少的匹配字符。如上面使用模式p匹配字符串str,结果就是匹配到:abc(ab*c)。
解决这个问题,可以采用:
正则引擎默认是贪婪的,当出现"*"时,它会尽量去匹配尽可能长的字符串。
一个用于修正以上问题的可能方案是用"*"的惰性代替贪婪性。你可以在"*"后面紧跟一个问号"?"来达到这一点
这告诉正则引擎,尽可能少的重复上一个字符。
如下面的例子:
#python 3. 6
#蔡军生
#http://blog.csdn.net/caimouse/article/details/51749579
#
from re_test_patterns import test_patterns
test_patterns(
'abbaabbba',
[('ab*?', 'a followed by zero or more b'),
('ab+?', 'a followed by one or more b'),
('ab??', 'a followed by zero or one b'),
('ab{3}?', 'a followed by three b'),
('ab{2,3}?', 'a followed by two to three b')],
)
输出结果如下:
'ab*?' (a followed by zero or more b)
'abbaabbba'
'a'
...'a'
....'a'
........'a'
'ab+?' (a followed by one or more b)
'abbaabbba'
'ab'
....'ab'
'ab??' (a followed by zero or one b)
'abbaabbba'
'a'
...'a'
....'a'
........'a'
'ab{3}?' (a followed by three b)
'abbaabbba'
....'abbb'
'ab{2,3}?' (a followed by two to three b)
'abbaabbba'
'abb'
....'abb'
深入浅出Numpy
http://edu.csdn.net/course/detail/6149
Python游戏开发入门
http://edu.csdn.net/course/detail/5690你也能动手修改C编译器
http://edu.csdn.net/course/detail/5582纸牌游戏开发
http://edu.csdn.net/course/detail/5538
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
RPG游戏从入门到精通
http://edu.csdn.net/course/detail/5246
WiX安装工具的使用
http://edu.csdn.net/course/detail/5207
俄罗斯方块游戏开发
http://edu.csdn.net/course/detail/5110
boost库入门基础
http://edu.csdn.net/course/detail/5029
Arduino入门基础
http://edu.csdn.net/course/detail/4931
Unity5.x游戏基础入门
http://edu.csdn.net/course/detail/4810
TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
TensorFlow入门基本教程
http://edu.csdn.net/course/detail/4369
C++标准模板库从入门到精通
http://edu.csdn.net/course/detail/3324
跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901
跟老菜鸟学python
http://edu.csdn.net/course/detail/2592
在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672
相关文章推荐
- python中如何使用正则表达式的非贪婪模式示例
- Python正则表达式的贪婪模式和非贪婪模式
- python 正则表达式贪婪模式与非贪婪模式
- python如何使用正则表达式的前向、后向搜索及前向搜索否定模式详解
- python里使用正则表达式的重复模式
- python里使用正则表达式的后向搜索否定模式
- python里使用正则表达式的ASCII模式
- python里使用正则表达式的非捕获模式
- python里使用正则表达式的前向搜索否定模式
- python 正则表达式 贪婪模式的简介和匹配时的几种模式
- python里使用正则表达式的详细模式(Verbose mode expression)
- php正则表达式中的非贪婪模式匹配的使用
- Python3正则表达式(三)贪婪模式与非贪婪模式
- python中使用正则表达式的后向搜索肯定模式(推荐)
- python里使用正则表达式的非捕获模式实例二
- 正则表达式非贪婪模式和取反字符类的使用区别
- python里使用正则表达式的前向搜索或后向搜索模式
- 浅谈php正则表达式中的非贪婪模式匹配的使用
- Python 正则表达式里面 贪婪模式和非贪婪模式的区别
- 详解Python里使用正则表达式的ASCII模式