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.

  • 相关阅读:
    SpringCloud 学习之概述
    定位慢查询
    中止线程
    笨办法41学会说面向对象【pyinstaller安装使用
    pip安装
    笨办法40模块, 类和对象class
    笨办法39字典dict
    笨办法38列表操作
    笨办法35分支和函数
    笨办法34访问列表元素(列表方法)
  • 原文地址:https://www.cnblogs.com/zou272/p/7822790.html
Copyright © 2011-2022 走看看