Python 正则表达式
2016-05-11 01:43
555 查看
def match(pattern, string, flags=0): """Try to apply the pattern at the start of the string, returning a match object, or None if no match was found.""" return _compile(pattern, flags).match(string)
import re print (re.match('www','www.baidu.com'))
结果:<_sre.SRE_Match object; span=(0, 3), match='www'>
print (re.match('www','www.baidu.com').group())
结果:www
print (re.match('w.b','www.baidu.com')) 结果:None
结论:匹配开始的字符串,返回一个object的对象,要想查看匹配的结果,需要用到group()方法
def fullmatch(pattern, string, flags=0): """Try to apply the pattern to all of the string, returning a match object, or None if no match was found.""" return _compile(pattern, flags).fullmatch(string)
print (re.fullmatch('www.baidu.com','www.baidu.com').group()) 结果:www.baidu.com
print (re.fullmatch('www','www.baidu.com')) 结果:None
def search(pattern, string, flags=0): """Scan through string looking for a match to the pattern, returning a match object, or None if no match was found.""" return _compile(pattern, flags).search(string)
print (re.search('w.b','www.baidu.com')) 结果:<_sre.SRE_Match object; span=(2, 5), match='w.b'>
print (re.search('w.b','www.baidu.com').group()) 结果:w.b
print (re.search('wwa','www.baidu.com')) 结果:None
def 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.""" return _compile(pattern, flags).sub(repl, string, count)
import re inputStr = "hello 111 world 222" outputStr = re.sub("\d+","333",inputStr) print(outputStr) 结果:hello 333 world 333
inputStr = "hello 111 world 222 hello hello hello hello" outputStr = re.sub("(llo)","333",inputStr,2) print(outputStr) 结果:he333 111 world 222 he333 hello hello hello
import re; inputStr = "hello-111 world 222" outputStr = re.sub("\w+-\d+","333",inputStr) print(outputStr) 结果:333 world 222 结论:\w+表示一个字母或数字组成的字符串,\d+表示至少一个数字,-表示中间的连接符-
inputStr = "hello-111 world 222" outputStr = re.sub("[A-Za-z]\w*","333",inputStr) print(outputStr) 结果:333-111 333 222 结论:前面是字母,后面是字母或者数字
inputStr = "hello-111-222" outputStr = re.sub("\d{3}-\d{3}","333",inputStr) print(outputStr) 结果:hello-333 结论:三个数字,三个数字,中间用-连接起来
inputStr = "lij1@hkbea.com" outputStr = re.sub("\w+@\w+\.com","333",inputStr) print(outputStr) 结果:333
相关文章推荐
- 《Python核心编程》第6章 习题
- 科学计算工具-ipython
- python socket理论知识
- python解析XML之ElementTree
- python(一)读取外包txt文档,并创建数据
- Python3控制路由器――使用requests重启极路由.py
- Python3使用requests发闪存的方法
- Python匹配中文的正则表达式
- python数据持久存储:pickle
- 为什么在Python里推荐使用多进程而不是多线程?
- Python的GIL是什么鬼,多线程性能究竟如何
- spark分布式平台下python环境的搭建
- Jieba分词Python简单实现
- python设计模式(二)--策略模式(上)
- python设计模式(二)--策略模式(中)
- tornado总结5-登录与cookies
- qqzeng-ip.dat IP库读取python版
- Python Lib Queue.py
- Python condition
- Python:Windows7 (64位)系统下安装Scrapy详细教程