zoukankan      html  css  js  c++  java
  • python里使用正则表达式的后向搜索肯定模式

    在前面学习了比较多模式,有前向搜索的,也有后向搜索的,有肯定模式的,也有否定模式的。这次再来学习一个,就是后向搜索肯定模式,意思就是说已经扫描过了的字符串,还想后悔去看一下,是否可以匹配。它的语法是:(?<=pattern)。比如下面的例子,就是用来识别Twitter的账号,但它这种模式只会匹配,不会出现在匹配的字符串中,如下:
     

    [python] view plain copy
     
    1. #python 3.6  
    2. #  
    3. import re  
    4.   
    5. twitter = re.compile(  
    6.     ''''' 
    7.     # A twitter handle: @username 
    8.     (?<=@) 
    9.     ([wd_]+)       # username 
    10.     ''',  
    11.     re.VERBOSE)  
    12.   
    13. text = '''''This text includes two Twitter handles. 
    14. One for @caimouse, and one for the author, @caijunsheng. 
    15. '''  
    16.   
    17. print(text)  
    18. for match in twitter.findall(text):  
    19.     print('Handle:', match)  



     结果输出如下:
     This text includes two Twitter handles.
    One for @caimouse, and one for the author, @caijunsheng.

  • 相关阅读:
    解决IE8下VS2005,VS2008一些向导提示脚本错误问题
    12-7
    12.4
    写在十一月的尾巴
    11.28
    htm&css 颜色的浮动
    11.27
    11.26
    html基础——表格练习
    html基础——div/span
  • 原文地址:https://www.cnblogs.com/zou272/p/7822790.html
Copyright © 2011-2022 走看看