- 浏览: 641254 次
- 性别:
- 来自: 淮安
文章分类
- 全部博客 (260)
- Java (0)
- vim (9)
- python (86)
- AdobePDF (2)
- mysql (10)
- Emacs (12)
- JavaScript (18)
- postgresql (2)
- windows (4)
- Eclipse (1)
- debian (4)
- 浏览器 (2)
- html (1)
- rhel (1)
- Linux (4)
- SVN (2)
- Ruby (30)
- ERP (8)
- my_linux_config (2)
- C++ (2)
- CentOS (5)
- Ubuntu (12)
- httpd (3)
- nginx (1)
- CSS (1)
- Agile (1)
- C (3)
- Redmine (2)
- 面试题 (1)
- 收集 (7)
- 架构 (1)
- 服务器 (2)
- logarithms (1)
- 数学 (1)
最新评论
-
hymzjsw:
python 变量命名规范 -
IWSo:
...
mysql #1170错误(42000) BLOB/TEXT Column Used in Key Specification Without a Key Le -
wl59138528:
由于Python臭名昭著的GIL问题,OpenERP 6.1以 ...
OpenERP 部署环境使用说明 -
greybeard:
xiaoyao3857 写道怎么看着一大堆,似乎中间有些东西重 ...
python 变量命名规范 -
xiaoyao3857:
怎么看着一大堆,似乎中间有些东西重复说了吧
python 变量命名规范
re.match
re.match 尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词。
import re
text = "JGood is a handsome boy, he is cool, clever, and so on..."
m = re.match(r"(\w+)\s", text)
if m:
print m.group(0), '\n', m.group(1)
else:
print 'not match'
import re text = "JGood is a handsome boy, he is cool, clever, and so on..." m = re.match(r"(\w+)\s", text) if m: print m.group(0), '\n', m.group(1) else: print 'not match'
re.match的函数原型为:re.match(pattern, string, flags)
第一个参数是正则表达式,这里为"(\w+)\s",如果匹配成功,则返回一个Match,否则返回一个None;
第二个参数表示要匹配的字符串;
第三个参数是标致位,用于控制正则表达式的匹配方式,如:是否区分大小写,多行匹配等等。
re.search
re.search函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回,如果字符串没有匹配,则返回None。
import re
text = "JGood is a handsome boy, he is cool, clever, and so on..."
m = re.search(r'\shan(ds)ome\s', text)
if m:
print m.group(0), m.group(1)
else:
print 'not search'
import re text = "JGood is a handsome boy, he is cool, clever, and so on..." m = re.search(r'\shan(ds)ome\s', text) if m: print m.group(0), m.group(1) else: print 'not search'
re.search的函数原型为: re.search(pattern, string, flags)
每个参数的含意与re.match一样。
re.match与re.search的区别:re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。
re.sub
re.sub用于替换字符串中的匹配项。下面一个例子将字符串中的空格 ' ' 替换成 '-' :
import re
text = "JGood is a handsome boy, he is cool, clever, and so on..."
print re.sub(r'\s+', '-', text)
import re text = "JGood is a handsome boy, he is cool, clever, and so on..." print re.sub(r'\s+', '-', text)
re.sub的函数原型为:re.sub(pattern, repl, string, count)
其中第二个函数是替换后的字符串;本例中为'-'
第四个参数指替换个数。默认为0,表示每个匹配项都替换。
re.sub还允许使用函数对匹配项的替换进行复杂的处理。如:re.sub(r'\s', lambda m: '[' + m.group(0) + ']', text, 0);将字符串中的空格' '替换为'[ ]'。
re.split
可以使用re.split来分割字符串,如:re.split(r'\s+', text);将字符串按空格分割成一个单词列表。
re.findall
re.findall可以获取字符串中所有匹配的字符串。如:re.findall(r'\w*oo\w*', text);获取字符串中,包含'oo'的所有单词。
re.compile
可以把正则表达式编译成一个正则表达式对象。可以把那些经常使用的正则表达式编译成正则表达式对象,这样可以提高一定的效率。下面是一个正则表达式对象的一个例子:
import re
text = "JGood is a handsome boy, he is cool, clever, and so on..."
regex = re.compile(r'\w*oo\w*')
print regex.findall(text) #查找所有包含'oo'的单词
print regex.sub(lambda m: '[' + m.group(0) + ']', text) #将字符串中含有'oo'的单词用[]括起来。
import re text = "JGood is a handsome boy, he is cool, clever, and so on..." regex = re.compile(r'\w*oo\w*') print regex.findall(text) #查找所有包含'oo'的单词 print regex.sub(lambda m: '[' + m.group(0) + ']', text) #将字符串中含有'oo'的单词用[]括起来。
更详细的内容,可以参考Python手册。
发表评论
-
列表解析和生成器表达式
2012-06-06 08:14 1399列表解析: 括在方括号 >& ... -
Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
2012-03-06 22:10 59256转自: http://www.jb51.net/article ... -
Django Meta内部类选项
2012-03-06 00:13 1593转自:http://www.onepub.net/2012/0 ... -
python --- locals()函数
2012-03-02 12:21 1440>>> help(locals) Hel ... -
python __file__ 与相对路径
2012-03-02 11:45 1456转自:http://taoyh163.blog.163.com ... -
python最常用函数
2012-03-02 10:51 6532转自:http://blog.163.com/yang ... -
Python ---- 处理Excel
2012-02-25 12:17 15021. Ubuntu下相关的库的安装: $ sudo ap ... -
python --- Python中的callable 函数
2012-02-25 11:41 8318转自: http://archive.cnblogs. ... -
Python --- __call__ (可调用对象)
2012-02-25 11:41 2116转自: http://hi.baidu.com/feng221 ... -
Python---很强悍的property,绝对的强大
2012-02-25 11:41 28824转自:http://www.cnblogs.com/lovem ... -
什么是po文件?
2012-02-25 11:40 2319转自: http://hi.baidu.com/gre ... -
Python----很强悍的API
2012-02-21 15:26 6301. Python 自带的 gettext 标准模块 & ... -
python ---- 包中的__init__.py具体用法
2012-02-21 10:51 2432The __init__.py files are requi ... -
Python ---- 各种包的收集
2012-02-21 10:52 9341. psutil 是一个 Python模块用来获取正 ... -
Python---我还不知道的寻找他用法的地方
2012-02-21 10:52 9401. exec exec('xx') 可以直接执行xx ... -
Python ---- Sets
2012-02-20 13:06 899Python also includes a data typ ... -
Python---Tuple special characteristics
2012-02-20 10:27 1065A special problem is the constr ... -
python 如此灵活的使用filter, map, reduce, sum
2012-02-18 12:22 2616Functional Programming Tools T ... -
Python表示队列
2012-02-18 11:57 1173It is also possible to use a li ... -
这么酷的lambda用法
2012-02-18 11:12 999Lambda Forms By popular demand ...
相关推荐
re模块中的常用函数 1. re.match(pattern, string, flags=0) 这个函数尝试从字符串的起始位置匹配一个模式,如果匹配成功,就返回一个匹配对象,否则返回None。其中,pattern是正则表达式,string是要匹配的字符串...
re模块主要函数的了解,对应文件:StandardLibrary10。文章:https://blog.csdn.net/yysyangyangyangshan/article/details/85149128
print(‘a’,’b‘,’c’) 打印多个元素处理 print(‘1+1=’,1+1) 前面字符串,后面是计算结果 print(objects, sep=’ ‘, end=’’) 打印(内容,用什么分割内容,用什么结尾) a = input(’’) ...
re模块下的函数 compile(pattern):创建模式对象 import re pat=re.compile('A') m=pat.search('CBA') #等价于 re.search('A','CBA') print m <_sre.SRE_Match object at 0x9d690c8> #匹配到了,返回Match...
Re模块是python的内置模块,提供了正则表达式在python中的所有用法,默认安装位置在python根目录下的Lib文件夹(如 ..\Python\Python37\Lib)。主要提供了3大类字符串操作方法: 字符查找/匹配 字符替换 字符分割...
indall函数返回的总是正则表达式在字符串中所有匹配结果的列表list,此处主要讨论列表中“结果”的展现方式,即findall中返回列表中每个元素包含的信息。 1.当给出的正则表达式中带有多个括号时,列表的元素为多个...
Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,在文本解析、复杂字符串分析和信息提取时是一个非常有用的工具,下面我主要总结了re的常用方法
主要介绍了Python re 模块findall() 函数返回值展现方式解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本文研究的是re模块findall()函数的相关内容,首先看看实例代码: >>> import re >>> s = adfad asdfasdf asdfas asdfawef asd adsfas >>> reObj1 = re.compile('((\w+)\s+\w+)') >>> reObj1.findall(s) [('adfad ...
\ “来代表特殊形式或用作转义字符,这里跟Python的语法冲突,因此,Python用” \\\\ “表示正则表达式中的” \ “,因为正则表达式中如果要匹配” \ “,需要用\来转义,变成” \\ “,而Python语法中又需要对字符...
re模块 re.compile、re.match、 re.search re 模块官方说明文档 正则匹配的时候,第一个字符是 r,表示 raw string 原生字符,意在声明字符串中间的特殊字符不用转义。 比如表示 ‘\n’,可以写 r’\n’,或者不适用...
PPT 讲述了正则表达式的使用方式以及如何使用python来操作正则表达式,内容简洁实用,适合平时查询,也适合PPT演讲使用~
本文实例讲述了Python3正则匹配re.split,re.finditer及re.findall函数用法。分享给大家供大家参考,具体如下: re.split re.finditer re.findall @(python3) 官方 re 模块说明文档 re.compile() 函数 编译正则...
* re:re模块用来对字符串进行正则表达式的匹配和替换。 * string:string模块包含大量有用的常量和函数用来处理字符串。也包含了新字符串格式的类。 * struct:struct模块被用来在python和二进制结构间实现转化。 *...
就其本质而言,正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现。使用这个小型语言,你可以为想要匹配的相应字符串集指定规则;该字符串集可能包含...
主要介绍了Python使用re模块实现信息筛选的方法,结合实例形式分析了Python正则re模块进行信息筛选操作的相关实现技巧及相关函数使用技巧,需要的朋友可以参考下
Python中的re模块提供了一个正则表达式引擎接口,它允许我们将正则表达式编译成模式对象,然后通过这些模式对象执行模式匹配搜索和字符串分割、子串替换等操作。re模块为这些操作分别提供了模块级别的函数以及相关类...
re模块中的常用函数 2.1 re.match() re.match()函数用于从字符串的开头匹配一个模式。如果匹配成功,返回一个匹配对象;否则返回None。 语法:re.match(pattern, string, flags=0) 其中,pattern是正则表达式,...