zoukankan      html  css  js  c++  java
  • python3.6入门到高阶(全栈) day023 正则表达式 re

    今日主要内容
    1. 正则表达式
    元字符
    . 匹配除换⾏符以外的任意字符
    w 匹配字⺟或数字或下划线
    s 匹配任意的空⽩符
    d 匹配数字
    匹配⼀个换⾏符
    匹配⼀个制表符
     匹配⼀个单词的结尾
    ^ 匹配字符串的开始
    $ 匹配字符串的结尾
    W 匹配⾮字⺟或数字或下划线
    D 匹配⾮数字
    S 匹配⾮空⽩符
    a|b 匹配字符a或字符b
    () 匹配括号内的表达式,也表示⼀个组
    [...] 匹配字符组中的字符
    [^...] 匹配除了字符组中字符的所有字符

    量词 限定符
    * {0,n} 重复零次或更多次
    + {1,n} 重复⼀次或更多次
    ? {0,1} 重复零次或⼀次
    {p} p次 重复n次
    {p,} p次或更多次 重复n次或更多次
    {p,q} 从p到q次 重复n到m次

    在量词中的*, +,{} 都属于贪婪匹配. 就是尽可能多的匹配到结果.
    .*
    .+

    .*?

    分组 ()
    换行
    \n
    .
    /
    ?

    2. re模块
    findall() 获取到匹配的所有内容
    lst = re.findall("m", "mai le fo len, mai ni mei!")
    print(lst) # ['m', 'm', 'm']

    lst = re.findall(r"d+", "5点之前. 你要给我5000万")
    print(lst) # ['5', '5000']
    finditer() 匹配到所有内容。 返回迭代器
    it = re.finditer("m", "mai le fo len, mai ni mei!")
    for el in it:
    print(el.group()) # 依然需要分组
    search() 搜索。查到了就返回( 会进⾏匹配. 但是如果匹配到了第⼀个结果.
    就会返回这个结果. 如果匹配不上search返回的则是None)
    ret = re.search(r'd', '5点之前. 你要给我5000万').group()
    print(ret) # 5

    match() 匹配. 从头开始匹配
    ret = re.match('a', 'abc').group()
    print(ret) # a

    r"(?P<name>正则)"

    其他操作

    split (加了括号()。 split会保留你切的刀)
    ret = re.split('[ab]', 'qwerafjbcd') # 先按'a'分割得到'qwer'和'fjbcd',在
    对'qwer'和'fjbcd'分别按'b'分割
    print(ret) # ['qwer', 'fj', 'cd']

    sub
    ret = re.sub(r"d+", "_sb_", "alex250taibai250wusir250ritian38") # 把字符串中
    的数字换成__sb__
    print(ret) # alex_sb_taibai_sb_wusir_sb_ritian_sb_

    ret = re.subn(r"d+", "_sb_", "alex250taibai250wusir250ritian38") # 将数字替
    换成'__sb__',返回元组(替换的结果,替换了多少次)
    print(ret) # ('alex_sb_taibai_sb_wusir_sb_ritian_sb_', 4)

    compile
    code = "for i in range(10):print(i)"
    c = compile(code, "", "exec") # 编译代码
    exec(c)

    search
    obj = re.compile(r'd{3}')
    ret = obj.search('abc123eeee').group() # 正则表达式对象调⽤search, 参数为待匹配的字符串
    print(ret) # 结果: 123
  • 相关阅读:
    luogu P1833 樱花 看成混合背包
    luogu P1077 摆花 基础记数dp
    luogu P1095 守望者的逃离 经典dp
    Even Subset Sum Problem CodeForces
    Maximum White Subtree CodeForces
    Sleeping Schedule CodeForces
    Bombs CodeForces
    病毒侵袭持续中 HDU
    病毒侵袭 HDU
    Educational Codeforces Round 35 (Rated for Div. 2)
  • 原文地址:https://www.cnblogs.com/wanxiangai/p/9971694.html
Copyright © 2011-2022 走看看