zoukankan      html  css  js  c++  java
  • 正则(re 模块)

    就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹配引擎执行。

    字符匹配(普通字符,元字符):

    1 普通字符:大多数字符和字母都会和自身匹配
                  >>> re.findall('alvin','yuanaleSxalexwupeiqi')
                          ['alvin'] 

    2 元字符:. ^ $ * + ? { } [ ] | ( )

    元字符之. ^ $ * + ? { }

     1 import re
     2  
     3 ret=re.findall('a..in','helloalvin')
     4 print(ret)#['alvin']
     5  
     6  
     7 ret=re.findall('^a...n','alvinhelloawwwn')
     8 print(ret)#['alvin']
     9  
    10  
    11 ret=re.findall('a...n$','alvinhelloawwwn')
    12 print(ret)#['awwwn']
    13  
    14  
    15 ret=re.findall('a...n$','alvinhelloawwwn')
    16 print(ret)#['awwwn']
    17  
    18  
    19 ret=re.findall('abc*','abcccc')#贪婪匹配[0,+oo]  
    20 print(ret)#['abcccc']
    21  
    22 ret=re.findall('abc+','abccc')#[1,+oo]
    23 print(ret)#['abccc']
    24  
    25 ret=re.findall('abc?','abccc')#[0,1]
    26 print(ret)#['abc']
    27  
    28  
    29 ret=re.findall('abc{1,4}','abccc')
    30 print(ret)#['abccc'] 贪婪匹配

    注意:前面的*,+,?等都是贪婪匹配,也就是尽可能匹配,后面加?号使其变成惰性匹配

    1 ret=re.findall('abc*?','abcccccc')
    2 print(ret)#['ab']

    元字符之字符集[]:

     1 #--------------------------------------------字符集[]
     2 ret=re.findall('a[bc]d','acd')
     3 print(ret)#['acd']
     4  
     5 ret=re.findall('[a-z]','acd')
     6 print(ret)#['a', 'c', 'd']
     7  
     8 ret=re.findall('[.*+]','a.cd+')
     9 print(ret)#['.', '+']
    10  
    11 #在字符集里有功能的符号: - ^ 
    12  
    13 ret=re.findall('[1-9]','45dha3')
    14 print(ret)#['4', '5', '3']
    15  
    16 ret=re.findall('[^ab]','45bdha3')
    17 print(ret)#['4', '5', 'd', 'h', '3']
    18  
    19 ret=re.findall('[d]','45bdha3')
    20 print(ret)#['4', '5', '3']

    元字符之转义符

    反斜杠后边跟元字符去除特殊功能,比如.
    反斜杠后边跟普通字符实现特殊功能,比如d

    d  匹配任何十进制数;它相当于类 [0-9]。
    D 匹配任何非数字字符;它相当于类 [^0-9]。
    s  匹配任何空白字符;它相当于类 [ fv]。
    S 匹配任何非空白字符;它相当于类 [^ fv]。
    w 匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。
    W 匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]
      匹配一个特殊字符边界,比如空格 ,&,#等

    1 ret=re.findall('I','I am LIST')
    2 print(ret)#[]
    3 ret=re.findall(r'I','I am LIST')
    4 print(ret)#['I']

    现在我们聊一聊\,先看下面两个匹配:

     1 #-----------------------------eg1:
     2 import re
     3 ret=re.findall('cl','abcle')
     4 print(ret)#[]
     5 ret=re.findall('c\l','abcle')
     6 print(ret)#[]
     7 ret=re.findall('c\\l','abcle')
     8 print(ret)#['c\l']
     9 ret=re.findall(r'c\l','abcle')
    10 print(ret)#['c\l']
    11  
    12 #-----------------------------eg2:
    13 #之所以选择是因为在ASCII表中是有意义的
    14 m = re.findall('blow', 'blow')
    15 print(m)
    16 m = re.findall(r'blow', 'blow')
    17 print(m)

    元字符之分组()

    1 m = re.findall(r'(ad)+', 'add')
    2 print(m)
    3  
    4 ret=re.search('(?P<id>d{2})/(?P<name>w{3})','23/com')
    5 print(ret.group())#23/com
    6 print(ret.group('id'))#23

    元字符之|

    1 ret=re.search('(ab)|d','rabhdg8sd')
    2 print(ret.group())#ab

    re模块下的常用方法

     1 import re
     2 #1
     3 re.findall('a','alvin yuan')    #返回所有满足匹配条件的结果,放在列表里
     4 #2
     5 re.search('a','alvin yuan').group()  #函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以
     6                                      # 通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。
     7  
     8 #3
     9 re.match('a','abc').group()     #同search,不过尽在字符串开始处进行匹配
    10  
    11 #4
    12 ret=re.split('[ab]','abcd')     #先按'a'分割得到''和'bcd',在对''和'bcd'分别按'b'分割
    13 print(ret)#['', '', 'cd']
    14  
    15 #5
    16 ret=re.sub('d','abc','alvin5yuan6',1)
    17 print(ret)#alvinabcyuan6
    18 ret=re.subn('d','abc','alvin5yuan6')
    19 print(ret)#('alvinabcyuanabc', 2)
    20  
    21 #6
    22 obj=re.compile('d{3}')
    23 ret=obj.search('abc123eeee')
    24 print(ret.group())#123
    1 import re
    2 ret=re.finditer('d','ds3sy4784a')
    3 print(ret)        #<callable_iterator object at 0x10195f940>
    4  
    5 print(next(ret).group())
    6 print(next(ret).group())

    注意:

    1 import re
    2  
    3 ret=re.findall('www.(baidu|oldboy).com','www.oldboy.com')
    4 print(ret)#['oldboy']     这是因为findall会优先把匹配结果组里内容返回,如果想要匹配结果,取消权限即可
    5  
    6 ret=re.findall('www.(?:baidu|oldboy).com','www.oldboy.com')
    7 print(ret)#['www.oldboy.com']

    补充:

     1 import re
     2 
     3 print(re.findall("<(?P<tag_name>w+)>w+</(?P=tag_name)>","<h1>hello</h1>"))
     4 print(re.search("<(?P<tag_name>w+)>w+</(?P=tag_name)>","<h1>hello</h1>"))
     5 print(re.search(r"<(w+)>w+</1>","<h1>hello</h1>"))
     6 
     7 
     8 
     9 #匹配出所有的整数
    10 import re
    11 
    12 #ret=re.findall(r"d+{0}]","1-2*(60+(-40.35/5)-(-4*3))")
    13 ret=re.findall(r"-?d+.d*|(-?d+)","1-2*(60+(-40.35/5)-(-4*3))")
    14 ret.remove("")
    15 
    16 print(ret)
  • 相关阅读:
    vue项目中使用mockjs模拟接口返回数据
    Node.js:Express 框架
    Node.js:Web模块、文件系统
    Node.js:get/post请求、全局对象、工具模块
    Node.js:常用工具、路由
    echarts使用记录(二)legend翻页,事件,数据集,设置y轴最大/小值,让series图形从右侧出往左移动
    Node.js:模块系统、函数
    ElementUI表单验证使用
    高级程序员职责
    Git:fatal: The remote end hung up unexpectedly
  • 原文地址:https://www.cnblogs.com/horror/p/9383573.html
Copyright © 2011-2022 走看看