zoukankan      html  css  js  c++  java
  • 学习python语言-----re模块

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

    1 普通字符:

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

    2 元字符:

    . ^ $ * + ? { } [ ] | ( )

    1)元字符之 . ^ $ * + ? { }
    import re
     
    ret=re.findall('a..in','helloalvin')# .代表任意单个字符,但不包括换行符
    print(ret)#['alvin']
     
     
    ret=re.findall('^a...n','alvinhelloawwwn')# ^代表只匹配开头的字符串
    print(ret)#['alvin']
     
     
    ret=re.findall('a...n$','alvinhelloawwwn')# $代表只匹配结尾的字符串
    print(ret)#['awwwn']
     
     
    ret=re.findall('abc*','abcccc')# *贪婪匹配[0,+oo]  
    print(ret)#['abcccc']
     
    ret=re.findall('abc+','abccc')# +[1,+oo]
    print(ret)#['abccc']
     
    ret=re.findall('abc?','abccc')# ?[0,1]要么有要么没有
    print(ret)#['abc']
     
     
    ret=re.findall('abc{1,4}','abccc')# 匹配最后一个字符1-4次
    print(ret)#['abccc'] 贪婪匹配
    2)元字符之字符集[](或):
    ret=re.findall('a[bc]d','acd')
    print(ret)#['acd']
     
    ret=re.findall('[a-z]','acd')
    print(ret)#['a', 'c', 'd']
     
    ret=re.findall('[.*+]','a.cd+')
    print(ret)#['.', '+']
     
    #在字符集里有功能的符号: - ^ ,其余的在[]中就是普通的字符串
     
    ret=re.findall('[1-9]','45dha3')
    print(ret)#['4', '5', '3']
     
    ret=re.findall('[^ab]','45bdha3')
    print(ret)#['4', '5', 'd', 'h', '3']
     
    ret=re.findall('[d]','45bdha3')
    print(ret)#['4', '5', '3']
    3)元字符之转义符
    import re
    ret=re.findall('cl','abcle')
    print(ret)#[]
    ret=re.findall('c\l','abcle')
    print(ret)#[]
    ret=re.findall('c\\l','abcle')
    print(ret)#['c\l']
    ret=re.findall(r'c\l','abcle')
    print(ret)#['c\l']
     
    #-----------------------------eg2:
    #之所以选择是因为在ASCII表中是有意义的
    m = re.findall('blow', 'blow')
    print(m)
    m = re.findall(r'blow', 'blow')# ‘r’表示该字符串是一个原生字符串,python不更改其中的内容,re模块直接进行调用
    print(m)

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

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

    4)元字符之分组()
    m = re.findall(r'(ad)+', 'add')
    print(m)
     
    ret=re.search('(?P<id>d{2})/(?P<name>w{3})','23/com')
    print(ret.group())#23/com
    print(ret.group('id'))#23
    print(ret.group('name'))#com
    5)元字符之|
    ret=re.search('(ab)|d','rabhdg8sd')#或的意思
    print(ret.group())#ab

    re模块下的常用方法

    import re
    #1
    re.findall('a','alvin yuan')    #返回所有满足匹配条件的结果,放在列表里
    #2
    re.search('a','alvin yuan').group()  #函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以
                                         # 通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。
     
    #3
    re.match('a','abc').group()     #同search,不过尽在字符串开始处进行匹配
     
    #4
    ret=re.split('[ab]','abcd')     #先按'a'分割得到''和'bcd',在对''和'bcd'分别按'b'分割
    print(ret)#['', '', 'cd']
     
    #5
    ret=re.sub('d','abc','alvin5yuan6',1)
    print(ret)#alvinabcyuan6
    ret=re.subn('d','abc','alvin5yuan6')
    print(ret)#('alvinabcyuanabc', 2)
     
    #6
    obj=re.compile('d{3}')
    ret=obj.search('abc123eeee')
    print(ret.group())#123
  • 相关阅读:
    9.11 eventbus
    9.10,,,实现new instanceof apply call 高阶函数,偏函数,柯里化
    9.9 promise实现 写完了传到gitee上面了,这里这个不完整
    9.5cors配置代码
    9.5 jsonp 实现
    9.5 http tcp https总结
    9.3 es6 class一部分 and es5 class 发布订阅
    8.30 cookie session token jwt
    8.30vue响应式原理
    warning: LF will be replaced by CRLF in renard-wx/project.config.json. The file will have its original line endings in your working directory
  • 原文地址:https://www.cnblogs.com/yujin123456/p/9671748.html
Copyright © 2011-2022 走看看