Grammar:

re.sub(pattern, repl, string[, count])

使用repl替换string中每一个匹配的子串后返回替换后的字符串。
当repl是一个字符串时,可以使用\id或\g、\g引用分组,但不能使用编号0。
当repl是一个方法时,这个方法应当只接受一个参数(Match对象),并返回一个字符串用于替换(返回的字符串中不能再引用分组)。
count用于指定最多替换次数,不指定时全部替换。

re.subn(pattern, repl, string[, count])

返回 (sub(repl, string[, count]), 替换次数)。

Case:

 #coding=utf-8

 import re

 str = "https://i.cnb1logs.co2m/Edi3tPosts.asp4x?opt=999"

 pattern=re.compile(r'(\.)')
print '\. :' ,re.sub(pattern,'-',str) pattern=re.compile(r'\/([^*]+)\/')
print '\/([^*]+)\/ :' ,re.sub(pattern,r'<em>\1<em>',str) pattern = re.compile(r'(\w+)(\w+)(\d+)')
#先切片测试
print re.split(pattern,str)
print re.sub(pattern,r'\3 \1',str)
#subn统计sub替换次数
print re.subn(pattern,r'\3 \1',str)

Output

 \.          : https://i-cnb1logs-co2m/Edi3tPosts-asp4x?opt=999
\/([^*]+)\/ : https:<em>/i.cnb1logs.co2m<em>Edi3tPosts.asp4x?opt=999
['https://i.', 'cn', 'b', '', 'logs.', 'c', 'o', '', 'm/', 'Ed', 'i', '', 'tPosts.', 'as', 'p', '', 'x?opt=', '', '', '', '']
https://i.1 cnlogs.2 cm/3 EdtPosts.4 asx?opt=9 9
('https://i.1 cnlogs.2 cm/3 EdtPosts.4 asx?opt=9 9', 5) ***Repl Closed***

quote:http://cuiqingcai.com/977.html

python 正则表达式 re.sub & re.subn的更多相关文章

  1. Python 正则表达式之 sub 和 subn函数的使用

    re.sub() 函数的功能 re是reguler expressioin的缩写,表示正则表达式 sub 是 substitute 的缩写,表示替换: re.sub是个正则表达式方面的函数,用来实现通 ...

  2. python正则表达式re

    Python正则表达式: re 正则表达式的元字符有. ^ $ * ? { [ ] | ( ).表示任意字符[]用来匹配一个指定的字符类别,所谓的字符类别就是你想匹配的一个字符集,对于字符集中的字符可 ...

  3. Python正则表达式详解

    我用双手成就你的梦想 python正则表达式 ^ 匹配开始 $ 匹配行尾 . 匹配出换行符以外的任何单个字符,使用-m选项允许其匹配换行符也是如此 [...] 匹配括号内任何当个字符(也有或的意思) ...

  4. 比较详细Python正则表达式操作指南(re使用)

    比较详细Python正则表达式操作指南(re使用) Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式.Python 1.5之前版本则是通过 regex 模块提供 E ...

  5. Python正则表达式学习摘要及资料

    摘要 在正则表达式中,如果直接给出字符,就是精确匹配. {m,n}? 对于前一个字符重复 m 到 n 次,并且取尽可能少的情况 在字符串'aaaaaa'中,a{2,4} 会匹配 4 个 a,但 a{2 ...

  6. Python 正则表达式-OK

    Python正则表达式入门 一. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分. 正则表达式是用于处理字符串的强大工具, 拥有自己独特的语法以及一个独立的处理引擎, 效率上 ...

  7. Python天天美味(15) - Python正则表达式操作指南(re使用)(转)

    http://www.cnblogs.com/coderzh/archive/2008/05/06/1185755.html 简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格 ...

  8. Python正则表达式Regular Expression基本用法

    资料来源:http://blog.csdn.net/whycadi/article/details/2011046   直接从网上资料转载过来,作为自己的参考.这个写的很清楚.先拿来看看. 1.正则表 ...

  9. [转] Python正则表达式指南

    本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例.本文的内容不包括如何编写高效的正则表达式.如何优化正则表达式,这些主题请查看其他教程 ...

随机推荐

  1. java中数组的数组问题

    int[] arr = new int[10]; int[] arr2 = arr; arr[1] = 10; arr2[1] = 20; System.out.println(arr[1]); 上面 ...

  2. netserver启动时报错 "Unable to start netserver with 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC'"

    netperf启动netserver时报错 "Unable to start netserver with 'IN(6)ADDR_ANY' port '12865' and family A ...

  3. ln -在文件之间建立连接

    总览 ln [options] source [dest] ln [options] source...directory POSIX 选项: [-f] GNU 选项(缩写): [-bdfinsvF] ...

  4. Linux-day-1

    1. ls    作用:列出文件信息,默认为当前目录下 常用选项:         -a: 列出所有的文件,包括所有以.开头的隐藏文件         -d: 列出目录本身,并不包含目录中的文件    ...

  5. Educational Codeforces Round 55 (Rated for Div. 2) D. Maximum Diameter Graph (构造图)

    D. Maximum Diameter Graph time limit per test2 seconds memory limit per test256 megabytes inputstand ...

  6. thinkphp5.0.19 表单令牌

    助手函数token() [F:\phpStudy\WWW\csweb\thinkphp\helper.php] request类token()方法 [F:\phpStudy\WWW\csweb\thi ...

  7. Delphi--长线程

    { 长线程, 开启:随应用程序启用而启动 关闭:岁应用程序关闭而结束 } unit uLongThread; interface uses Classes, ADODB, DB, ActiveX, S ...

  8. js-进度条-动画

    效果图如下:       HTML: <h1>js实现进度条</h1> <div id="warp"> <div id="inn ...

  9. B/S上传文件夹

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

  10. ORB an efficient alternative to SIFT or SURF

    AbstractFeature matching is at the base of many computer vision problems, such as object recognition ...