• re模块中常用的函数及分组


    match函数:

    从开始位置进行匹配,如果开始位置没有匹配到,就直接失败。

    text = "hello world"
    ret = re.match("h",text)
    print(ret.group())

    search函数:

    在字符串中找满足条件的字符,如果找到,就返回。说白了,就是只会找到第一个满足条件的。

    text = "hello world ni hao!"
    ret = re.search("h",text)
    print(ret.group())

    匹配结果是 h,而不会出现两个 hh。

    findall函数:

    找出所有满足条件的字符,返回的是一个列表。

    text = "It's price $99,others' price is $10."
    ret = re.findall("$d+",text)
    print(ret)
    

    匹配结果:['$99', '$10'] (按匹配顺序输出)。

    sub函数:

    用来替换字符串,将匹配到的字符串替换为其他字符串。

    text = "It's price $99,others' price is $10."
    ret = re.sub("$d+","$0",text,1)
    print(ret)
    

    匹配结果:It's price $0,others' price is $10

    sub()函数中有四个参数:第一个是正则表达式,第二个是替换的字符串,第三个是目标字符串,第四个参数是替换的个数(默认将所有满足条件的替换掉)。

    split函数:

    使用正则表达式来分割字符串,返回一个列表。

    text = "hello world"
    ret = re.split("[^a-zA-Z]",text)
    print(ret)
    

     

    分组group:

    在正则表达式中,可以对过滤的字符串进行分组。分组使用圆括号的方式。

    1.group()和group(0)是等价的,返回的是整个满足条件的字符串。
    2.group()返回的是里面子组。索引从1开始。
    3.group(1)返回的是第一个子组,可以传入多个。

    text = "It's price $99,others' price is $10."
    ret = re.search(".*($d+).*($d+).",text)
    print(ret.group())
    print(ret.group(1))
    print(ret.group(2))
    print(ret.group(2,1))
    print(ret.groups(0))
    

    其中  print(ret.group())  和  print(ret.groups(0))  输出的结果是一样的:It's price $99,others' price is $10.

    而  print(ret.group(1))  print(ret.group(2))  print(ret.group(2,1))  为只提取满足圆括号的字符串,即为:$99  $10  $10 $99 。

  • 相关阅读:
    矩阵树定理
    随机乱搞算法
    数学
    BZOJ——3412: [Usaco2009 Dec]Music Notes乐谱
    BZOJ—— 3402: [Usaco2009 Open]Hide and Seek 捉迷藏
    洛谷—— P2884 [USACO07MAR]每月的费用Monthly Expense
    洛谷—— P2417 课程
    洛谷—— P1577 切绳子
    COGS 2598. 美丽的感觉
    10.23 模拟赛
  • 原文地址:https://www.cnblogs.com/zyde-2893/p/11198354.html
Copyright © 2020-2023  润新知