• python正则表达式 re (二)sub


    背景:

    re.sub是re模块重要的组成部分,并且功能也非常强大,主要功能实现正则的替换。
    re.sub定义:
    sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl. repl can be either a string or a callable;
    if a string, backslash escapes in it are processed. If it is
    a callable, it’s passed the match object and must return
    a replacement string to be used.
    主要的意思为:对字符串string按照正则表达式pattern,将string的匹配项替换成字符串repl。
    公式解析:
    pattern为表示正则中的模式字符串,
    repl为replacement,被替换的内容,repl可以是字符串,也可以是函数。
    string为正则表达式匹配的内容。
    count:由于正则表达式匹配到的结果是多个,使用count来限定替换的个数(顺序为从左向右),默认值为0,替换所有的匹配到的结果。
    flags是匹配模式,可以使用按位或’|’表示同时生效,也可以在正则表达式字符串中指定。(详情见链接)

    举例:

    1. >>> import re  
    2. >>>re.sub(r'w+','10',"ji 43 af,geq",2,flags=re.I)
    3. >>> '10 10 af,geq'

    详解:首先导入re模块,使用re.sub函数,r’w+’为正则表达式,匹配英文单词或数字,’10’为被替换的内容,”ji 43 af,geq”为re匹配的字符串内容,count为2 只替换前两个,flags=re.I 忽略大小写。
    输出部分自行理解。

  • 相关阅读:
    软件命名的几种常见方式
    软件过程与项目管理第一周作业
    DOS命令大全 系统管理员专用
    数据库事务的作用
    利用C#事务处理对数据库进行多重操作
    JSP标签分页实现
    使用自定义端口连接sql server2008
    Solr4.4.0的安装与配置
    Android中如何使用ViewPager实现类似laucher左右拖动效果
    Android中Timer使用方法
  • 原文地址:https://www.cnblogs.com/xiaojinniu425/p/7615011.html
Copyright © 2020-2023  润新知