python里使用正则表达式的组匹配是否成功之后再自引用

来源:互联网 发布:软件测试第二版 编辑:程序博客网 时间:2024/05/22 07:07
在前面学习了通过名称或组号来引用本身正则表达式里的组内容,可以实现前后关联式的相等判断。如果再更进一步,比如当前面组匹配成功之后,就选择一种模式来识别,而不匹配成功又选择另外一种模式进行识别,这相当于if...else...语句的选择。我们来学习这种新的语法:(?(id)yes-expression|no-expression)。其中id是表示组名称或组编号, yes-expression是当组匹配成功之后选择的正则表达式,而no-expression 是不匹配成功之后选择的正则表达式。如下例子:
#python 3.6#蔡军生 #http://blog.csdn.net/caimouse/article/details/51749579#import readdress = re.compile(    '''    ^    # A name is made up of letters, and may include "."    # for title abbreviations and middle initials.    (?P<name>       ([\w.]+\s+)*[\w.]+     )?    \s*    # Email addresses are wrapped in angle brackets, but    # only if a name is found.    (?(name)      # remainder wrapped in angle brackets because      # there is a name      (?P<brackets>(?=(<.*>$)))      |      # remainder does not include angle brackets without name      (?=([^<].*[^>]$))     )    # Look for a bracket only if the look-ahead assertion    # found both of them.    (?(brackets)<|\s*)    # The address itself: username@domain.tld    (?P<email>      [\w\d.+-]+       # username      @      ([\w\d.]+\.)+    # domain name prefix      (com|org|edu)    # limit the allowed top-level domains     )    # Look for a bracket only if the look-ahead assertion    # found both of them.    (?(brackets)>|\s*)    $    ''',    re.VERBOSE)candidates = [    u'Cai junsheng <Cai.junsheng@example.com>',    u'No Brackets first.last@example.com',    u'Open Bracket <first.last@example.com',    u'Close Bracket first.last@example.com>',    u'no.brackets@example.com',]for candidate in candidates:    print('Candidate:', candidate)    match = address.search(candidate)    if match:        print('  Match name :', match.groupdict()['name'])        print('  Match email:', match.groupdict()['email'])    else:        print('  No match')


结果输出如下:
Candidate: Cai junsheng <Cai.junsheng@example.com>
  Match name : Cai junsheng
  Match email: Cai.junsheng@example.com
Candidate: No Brackets first.last@example.com
  No match
Candidate: Open Bracket <first.last@example.com
  No match
Candidate: Close Bracket first.last@example.com>
  No match
Candidate: no.brackets@example.com
  Match name : None
  Match email: no.brackets@example.com

在这里,当name组出现时才会寻找括号< >,如果括号不成对就不匹配成功;如果name组不出现,就不需要括号,因此选择了另一个正则表达式。

深入浅出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


阅读全文
0 0