在前面学习过的正则表达式都是紧凑格式的表示方式,当你写的正则表达式比较长,比较复杂时,发现经常会写错,或者很难维护,遇到这种情况怎么办呢?这个不用急,聪明的设计人员早已经想到这种情况了,提供了一种叫做详细模式(Verbose mode expression)。当你使用这种模式时,可以给正则表达式添加注释,这样在维护时就不会像看天书了,同时也可以添加额外的空格,进行对齐排版。紧凑模式的例子:
详细模式的修改之后:
结果输出如下:
first.last@example.com Matches
first.last+category@gmail.com Matches
valid-address@mail.example.com Matches
not-valid@example.foo No match
深入浅出Numpy
五子棋游戏开发
http://edu.youkuaiyun.com/course/detail/5487
import re
address = re.compile('[\w\d.+-]+@([\w\d.]+\.)+(com|org|edu)')
candidates = [
u'first.last@example.com',
u'first.last+category@gmail.com',
u'valid-address@mail.example.com',
u'not-valid@example.foo',
]
for candidate in candidates:
match = address.search(candidate)
print('{:<30} {}'.format(
candidate, 'Matches' if match else 'No match')
)
详细模式的修改之后:
#python 3.6
#蔡军生
#http://blog.youkuaiyun.com/caimouse/article/details/51749579
#
import re
address = re.compile(
'''
[\w\d.+-]+ # username
@
([\w\d.]+\.)+ # domain name prefix
(com|org|edu) # TODO: support more top-level domains
''',
re.VERBOSE)
candidates = [
u'first.last@example.com',
u'first.last+category@gmail.com',
u'valid-address@mail.example.com',
u'not-valid@example.foo',
]
for candidate in candidates:
match = address.search(candidate)
print('{:<30} {}'.format(
candidate, 'Matches' if match else 'No match'),
)
结果输出如下:
first.last@example.com Matches
first.last+category@gmail.com Matches
valid-address@mail.example.com Matches
not-valid@example.foo No match
参数解释:X VERBOSE
该标志通过给予你更灵活的格式以便你将正则表达式写得更易于理解。当该标志被指定时,在 RE 字符串中的空白符被忽略,除非该空白符在字符类中或在反斜杠之後;这可以让你更清晰地组织和缩进 RE。它也可以允许你将注释写入 RE,这些注释会被引擎忽略;注释用 "#"号 来标识,不过该符号不能在字符串或反斜杠之後。