码迷,mamicode.com
首页 > 编程语言 > 详细

python re.sub详解

时间:2017-06-02 17:25:40      阅读:737      评论:0      收藏:0      [点我收藏+]

标签:intval   注意事项   dex   strong   引用   port   pattern   sub   mat   

re.sub(patternreplstringcount=0flags=0)

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

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

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

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

 

re.sub共有五个参数。

其中三个必选参数:patternreplstring

两个可选参数:countflags

 

1:

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

需要知道的是:

  • 反斜杠加数字(\N),则对应着匹配的组(matched group)
    • 比如\6,表示匹配前面pattern中的第6个group
    • 意味着,pattern中,前面肯定是存在对应的,第6个group,然后你后面也才能去引用

比如,想要处理:

hello crifan, nihao crifan

而想要把整个这样的字符串,换成crifanli且此处的,前后的crifan,肯定是一样的。

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

inputStr = "hello crifan, nihao crifan";

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

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

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

repl是字符串

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

即:

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

接着上面的举例:

想要把对应的:

hello crifan, nihao crifan

中的crifan提取出来,只剩:crifan

 

inputStr = "hello crifan, nihao crifan";

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

repl是函数

比如输入内容是:hello 123 world 456

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

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();

  

3:

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

 

4:

count

假如对于匹配到的内容,只处理其中一部分

比如对于:hello 123 world 456 nihao 789

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

 

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();

  

关于re.sub的注意事项:

语法:

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

对应的第二个参数是repl。

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

r"xxxx"

 

 

 

python re.sub详解

标签:intval   注意事项   dex   strong   引用   port   pattern   sub   mat   

原文地址:http://www.cnblogs.com/dancesir/p/6933768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!