zoukankan      html  css  js  c++  java
  • Python正则表达式

    https://docs.python.org/zh-cn/3/library/re.html

    https://zhuanlan.zhihu.com/p/68014839

    findall,search,sub,S

    python findall

    import re
    str = 'aabbabaabbaa'
    print(re.findall('a.b',str))
    #['aab', 'aab']
    
    print(re.findall('a*b',str))
    #['aab', 'b', 'ab', 'aab', 'b'] *匹配前面的字符0次或者多次
    
    print(re.findall('a.*b',str))
    #['aabbabaabb'] .* 贪心算法
    
    print(re.findall('a.*?b',str))
    #['aab', 'ab', 'aab'] .*?非贪心算法,遇到开始和结束就截取
    
    print(re.findall('a(.*?)b',str))
    #['a', '', 'a'] .*?非贪心算法,只保留括号内的内容
    
    
    str = '''aabbab
             aabbaa
             bb'''
    
    print(re.findall('a.*?b',str))
    #['aab', 'ab', 'aab']
    
    print(re.findall('a.*?b',str,re.S))
    #['aab', 'ab', 'aab', 'aa
             b'] re.S不会对
    进行中断
    
  • 相关阅读:
    第二次结对作业
    第一次结对作业
    第二次个人编程
    第一次编程作业
    第一篇随笔
    个人总结
    第三次个人作业
    第二次结对作业
    第一次结对作业
    第二次编程
  • 原文地址:https://www.cnblogs.com/python-study/p/14063877.html
Copyright © 2011-2022 走看看