在正则表达式里,想匹配一些字符中的一个,也就是说给出一个字符的集合,只要出现这个集合里任意的字符,都是成立的。比如[ab],就是将匹配任意出现a或b的字符。比如a[ab]+,它是贪婪模式,将会匹配所有是a后面的a或b的字符串,如abbaabbba。如果要改为非贪婪模式,要在后面添加?,如下面的例子:
结果输出如下:
'[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'
深入浅出Numpy
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
#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'