【背景】

Python中的正则表达式方面的功能,很强大。

其中就包括re.sub,实现正则的替换。

功能很强大,所以导致用法稍微有点复杂。

所以当遇到稍微复杂的用法时候,就容易犯错。

所以此处,总结一下,在使用re.sub的时候,需要注意的一些事情。

解释具体的注意事项之前,先把其具体的解释贴出来:

re.sub

re.sub(patternreplstringcount=0flags=0)

Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function; if it is a string, any backslash escapes in it are processed. That is, \n is converted to a single newline character, \r is converted to a carriage return, and so forth. Unknown escapes such as \j are left alone. Backreferences, such as \6, are replaced with the substring matched by group 6 in the pattern. For example:

>>> re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
... r'static PyObject*\npy_\1(void)\n{',
... 'def myfunc():')
'static PyObject*\npy_myfunc(void)\n{'

If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string. For example:

>>> def dashrepl(matchobj):
... if matchobj.group(0) == '-': return ' '
... else: return '-'
>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
'pro--gram files'
>>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
'Baked Beans & Spam'

The pattern may be a string or an RE object.

The optional argument count is the maximum number of pattern occurrences to be replaced; countmust be a non-negative integer. If omitted or zero, all occurrences will be replaced. Empty matches for the pattern are replaced only when not adjacent to a previous match, so sub('x*', '-', 'abc') returns '-a-b-c-'.

In addition to character escapes and backreferences as described above, \g<name> will use the substring matched by the group named name, as defined by the (?P<name>...) syntax. \g<number>uses the corresponding group number; \g<2> is therefore equivalent to \2, but isn’t ambiguous in a replacement such as \g<2>0\20 would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character '0'. The backreference \g<0> substitutes in the entire substring matched by the RE.

Changed in version 2.7: Added the optional flags argument.


re.sub的功能

re是regular expression的所写,表示正则表达式

sub是substitute的所写,表示替换;

re.sub是个正则表达式方面的函数,用来实现通过正则表达式,实现比普通字符串的replace更加强大的替换功能;

举个最简单的例子:

如果输入字符串是:

1
inputStr = "hello 111 world 111"

那么你可以通过

1
replacedStr = inputStr.replace("111", "222")

去换成

"hello 222 world 222"

但是,如果输入字符串是:

1
inputStr = "hello 123 world 456"

而你是想把123和456,都换成222

(以及其他还有更多的复杂的情况的时候),

那么就没法直接通过字符串的replace达到这一目的了。

就需要借助于re.sub,通过正则表达式,来实现这种相对复杂的字符串的替换:

1
replacedStr = re.sub("\d+", "222", inputStr)

当然,实际情况中,会有比这个例子更加复杂的,其他各种特殊情况,就只能通过此re.sub去实现如此复杂的替换的功能了。

所以,re.sub的含义,作用,功能就是:

对于输入的一个字符串,利用正则表达式(的强大的字符串处理功能),去实现(相对复杂的)字符串替换处理,然后返回被替换后的字符串

其中re.sub还支持各种参数,比如count指定要替换的个数等等。

下面就是来详细解释其各个参数的含义。

re.sub的各个参数的详细解释

re.sub共有五个参数。

其中三个必选参数:patternreplstring

两个可选参数:countflags

第一个参数:pattern

pattern,表示正则中的模式字符串,这个没太多要解释的。

需要知道的是:

  • 反斜杠加数字(\N),则对应着匹配的组(matched group)

    • 比如\6,表示匹配前面pattern中的第6个group
    • 意味着,pattern中,前面肯定是存在对应的,第6个group,然后你后面也才能去引用

比如,想要处理:

hello crifan, nihao crifan

且此处的,前后的crifan,肯定是一样的。

而想要把整个这样的字符串,换成crifanli

则就可以这样的re.sub实现替换:

1
2
3
inputStr = "hello crifan, nihao crifan";
replacedStr = re.sub(r"hello (\w+), nihao \1", "crifanli", inputStr);
print "replacedStr=",replacedStr; #crifanli

第二个参数:repl

repl,就是replacement,被替换,的字符串的意思。

repl可以是字符串,也可以是函数。

repl是字符串

如果repl是字符串的话,其中的任何反斜杠转义字符,都会被处理的。

即:

  • \n:会被处理为对应的换行符;
  • \r:会被处理为回车符;
  • 其他不能识别的转移字符,则只是被识别为普通的字符:
    • 比如\j,会被处理为j这个字母本身;
  • 反斜杠加g以及中括号内一个名字,即:\g<name>,对应着命了名的组,named group

接着上面的举例:

想要把对应的:

hello crifan, nihao crifan

中的crifan提取出来,只剩:

crifan

就可以写成:

1
2
3
inputStr = "hello crifan, nihao crifan";
replacedStr = re.sub(r"hello (\w+), nihao \1", "\g<1>", inputStr);
print "replacedStr=",replacedStr; #crifan

对应的带命名的组(named group)的版本是:

1
2
3
inputStr = "hello crifan, nihao crifan";
replacedStr = re.sub(r"hello (?P<name>\w+), nihao (?P=name)", "\g<name>", inputStr);
print "replacedStr=",replacedStr; #crifan

repl是函数

举例说明:

比如输入内容是:

hello 123 world 456

想要把其中的数字部分,都加上111,变成:

hello 234 world 567

那么就可以写成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【整理】详解Python中re.sub
 
Version:    2013-05-02
Author:     Crifan
Contact:    admin (at) crifan.com
"""
 
import re;
 
def pythonReSubDemo():
    """
        demo Pyton re.sub
    """
    inputStr = "hello 123 world 456";
     
    def _add111(matched):
        intStr = matched.group("number"); #123
        intValue = int(intStr);
        addedValue = intValue + 111; #234
        addedValueStr = str(addedValue);
        return addedValueStr;
         
    replacedStr = re.sub("(?P<number>\d+)", _add111, inputStr);
    print "replacedStr=",replacedStr; #hello 234 world 567
 
###############################################################################
if __name__=="__main__":
    pythonReSubDemo();

第三个参数:string

string,即表示要被处理,要被替换的那个string字符串。

没什么特殊要说明。

第四个参数:count

举例说明:

继续之前的例子,假如对于匹配到的内容,只处理其中一部分。

比如对于:

hello 123 world 456 nihao 789

只是像要处理前面两个数字:123,456,分别给他们加111,而不处理789,

那么就可以写成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【整理】详解Python中re.sub
 
Version:    2013-05-02
Author:     Crifan
Contact:    admin (at) crifan.com
"""
 
import re;
 
def pythonReSubDemo():
    """
        demo Pyton re.sub
    """
    inputStr = "hello 123 world 456 nihao 789";
     
    def _add111(matched):
        intStr = matched.group("number"); #123
        intValue = int(intStr);
        addedValue = intValue + 111; #234
        addedValueStr = str(addedValue);
        return addedValueStr;
         
    replacedStr = re.sub("(?P<number>\d+)", _add111, inputStr, 2);
    print "replacedStr=",replacedStr; #hello 234 world 567 nihao 789
 
###############################################################################
if __name__=="__main__":
    pythonReSubDemo();

第五个参数:flags

关于re.sub的注意事项

然后再来整理一些,关于re.sub的注意事项,常见的问题及解决办法:

要注意,被替换的字符串,即参数repl,是普通的字符串,不是pattern

注意到,语法是:

1
re.sub(pattern, repl, string, count=0, flags=0)

即,对应的第二个参数是repl。

需要你指定对应的r前缀,才是pattern:

r"xxxx"

不要误把第四个参数flag的值,传递到第三个参数count中了

否则就会出现我这里:

【已解决】Python中,(1)re.compile后再sub可以工作,但re.sub不工作,或者是(2)re.search后replace工作,但直接re.sub以及re.compile后再re.sub都不工作

遇到的问题:

当传递第三个参数,原以为是flag的值是,

结果实际上是count的值

所以导致re.sub不功能,

所以要参数指定清楚了:

1
replacedStr = re.sub(replacePattern, orignialStr, replacedPartStr, flags=re.I); # can omit count parameter

或:

1
replacedStr = re.sub(replacePattern, orignialStr, replacedPartStr, 1, re.I); # must designate count parameter

才可以正常工作。

详解Python中re.sub--转载的更多相关文章

  1. 举例详解Python中的split()函数的使用方法

    这篇文章主要介绍了举例详解Python中的split()函数的使用方法,split()函数的使用是Python学习当中的基础知识,通常用于将字符串切片并转换为列表,需要的朋友可以参考下   函数:sp ...

  2. 详解Python中内置的NotImplemented类型的用法

    它是什么? ? 1 2 >>> type(NotImplemented) <type 'NotImplementedType'> NotImplemented 是Pyth ...

  3. 详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  4. 详解python中@的用法

    python中@的用法 @是一个装饰器,针对函数,起调用传参的作用. 有修饰和被修饰的区别,‘@function'作为一个装饰器,用来修饰紧跟着的函数(可以是另一个装饰器,也可以是函数定义). 代码1 ...

  5. Python Deque 模块使用详解,python中yield的用法详解

    Deque模块是Python标准库collections中的一项. 它提供了两端都可以操作的序列, 这意味着, 你可以在序列前后都执行添加或删除. https://blog.csdn.net/qq_3 ...

  6. 详解Python中的__init__和__new__

    转载:https://my.oschina.net/liuyuantao/blog/747164 1.__init__ 方法是什么? 使用Python写过面向对象的代码的同学,可能对 __init__ ...

  7. 详解 Python 中的下划线命名规则

    在 python 中,下划线命名规则往往令初学者相当 疑惑:单下划线.双下划线.双下划线还分前后……那它们的作用与使用场景 到底有何区别呢?今天 就来聊聊这个话题. 1.单下划线(_) 通常情况下,单 ...

  8. 详解Python中的__init__和__new__(静态方法)

    一.__init__ 方法是什么? 使用Python写过面向对象的代码的同学,可能对 __init__ 方法已经非常熟悉了,__init__ 方法通常用在初始化一个类实例的时候.例如: #-*- co ...

  9. 详解Python中的下划线

    本文将讨论Python中下划线(_)字符的使用方法.我们将会看到,正如Python中的很多事情,下划线的不同用法大多数(并非所有)只是常用惯例而已. 单下划线(_) 通常情况下,会在以下3种场景中使用 ...

随机推荐

  1. 【BZOJ3275】Number 最小割

    [BZOJ3275]Number Description 有N个正整数,需要从中选出一些数,使这些数的和最大.若两个数a,b同时满足以下条件,则a,b不能同时被选1:存在正整数C,使a*a+b*b=c ...

  2. 增量式PID的matlab实现

    首先,增量式PID的实现公式: 式中 Δe(k)=e(k)-e(k-1) 进一步可以改写成 式中      . . 为了便于理解,也可写成: 式中e(k)为第k次采样时的设定值与实际值的差,e(k-1 ...

  3. javascript飞机大战-----004创建子弹对象

    /* 创建子弹:因为子弹不是只创建一个所以要用构造函数 注意一点:子弹发射的位置应该是英雄机的正中央的位置,所以需要传点东西进来 */ function Bullet(l,t){ this.l = l ...

  4. 传智播客京东商城移动web开发

    1.源码笔记 我的源码+笔记(很重要):链接: https://pan.baidu.com/s/1eScieps 密码: 3vyr 感谢传智播客项目相关视频:1.6天链接: https://pan.b ...

  5. pta习题集 5-10 切分表达式——写个tokenizer吧

    [先说点出题背景] 这个题是为低年级同学.学C语言的同学准备的,因为,对这部分同学,这个题目编写起来略有一点复杂.如果是高年级.学过了正则表达式(Regular Expression)的同学或者学过了 ...

  6. Oracle AWR之-enq: TX - allocate ITL entry

    今天收到压力测试期间awr报告,测试人员要我看看数据库是否有可以优化的地方,数据库服务器配置信息:CPU:32*8,内存:480g 单实例数据库:oracle 11.2.0.4.具体分析过程如下: 可 ...

  7. Oracle性能优化之oracle里表、索引、列的统计信息

    一.表的统计信息 表的统计信息用于描述表的详细信息,包括记录数(num_rows).表块的数量(blocks).平均行长度(avg_row_len)等典型维度.这些维度可以通过数据字典表DBA_TAB ...

  8. linux: convmv =-======pkgs.org

    convmv 不同系统文件名转化 windows: gbk linux: utf8 wget--url coding. vim ---encoding,termcode, fileencoding, ...

  9. ubuntu安装conda

    https://blog.csdn.net/menghuanbeike/article/details/79138651 你需要前往Anaconda的官网看下目前的下载地址: https://www. ...

  10. Team Formation---zoj3870(异或)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5518 题意就是有n个数,如果满足a^b > MAX(a, b) ...