在前面学习的正则表达式,都是针对一个字母或一个字的匹配模式。如果有一天你想匹配两个字母或者两个字组成单词的匹配,会怎么办呢?使用前面的方法显然办不到。因为前面是一个字母或一个字的匹配,不具备多个字母一个整体的考虑,所以聪明的人们想到,可以使用一个括号来把一个单词括起来,这样就可以把单词作为一个整体进行匹配了。如下面的例子:
输出结果如下:
'a(ab)' (a followed by literal ab)
'abbaaabbbbaaaaa'
....'aab'
'a(a*b*)' (a followed by 0-n a and 0-n b)
'abbaaabbbbaaaaa'
'abb'
...'aaabbbb'
..........'aaaaa'
'a(ab)*' (a followed by 0-n ab)
'abbaaabbbbaaaaa'
'a'
...'a'
....'aab'
..........'a'
...........'a'
............'a'
.............'a'
..............'a'
'a(ab)+' (a followed by 1-n ab)
'abbaaabbbbaaaaa'
深入浅出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(
'abbaaabbbbaaaaa',
[('a(ab)', 'a followed by literal ab'),
('a(a*b*)', 'a followed by 0-n a and 0-n b'),
('a(ab)*', 'a followed by 0-n ab'),
('a(ab)+', 'a followed by 1-n ab')],
)
输出结果如下:
'a(ab)' (a followed by literal ab)
'abbaaabbbbaaaaa'
....'aab'
'a(a*b*)' (a followed by 0-n a and 0-n b)
'abbaaabbbbaaaaa'
'abb'
...'aaabbbb'
..........'aaaaa'
'a(ab)*' (a followed by 0-n ab)
'abbaaabbbbaaaaa'
'a'
...'a'
....'aab'
..........'a'
...........'a'
............'a'
.............'a'
..............'a'
'a(ab)+' (a followed by 1-n ab)
'abbaaabbbbaaaaa'
....'aab'