Python学习笔记(十):Python正则表达式
来源:互联网 发布:新版淘宝联盟自己购买 编辑:程序博客网 时间:2024/05/21 09:06
http://developer.51cto.com/art/201003/188586.htm
Python正则表达式十种相关的匹配方法
Python正则表达式需要各种各样的匹配,但是我们不能盲目的进行相匹配,下面就向大家介绍经常遇到的十种Python正则表达式匹配方式,希望大家有所收获。
1.测试Python正则表达式是否 匹配字符串的全部或部分
- regex=ur"..." #正则表达式
- if re.search(regex, subject):
- do_something()
- else:
- do_anotherthing()
2.测试Python正则表达式是否匹配整个字符串
- regex=ur"...\Z" #正则表达式末尾以\Z结束
- if re.match(regex, subject):
- do_something()
- else:
- do_anotherthing()
3. 创建一个匹配对象,然后通过该对象获得匹配细节
- regex=ur"..." #正则表达式
- match = re.search(regex, subject)
- if match:
- # match start: match.start()
- # match end (exclusive): match.end()
- # matched text: match.group()
- do_something()
- else:
- do_anotherthing()
4.获取Python正则表达式所匹配的子串
- regex=ur"..." #正则表达式
- match = re.search(regex, subject)
- if match:
- result = match.group()
- else:
- result = ""
5. 获取捕获组所匹配的子串
- regex=ur"..." #正则表达式
- match = re.search(regex, subject)
- if match:
- result = match.group(1)
- else:
- result = ""
6. 获取有名组所匹配的子串
- regex=ur"..." #正则表达式
- match = re.search(regex, subject)
- if match:
- result = match.group("groupname")
- else:
- result = ""
7. 将字符串中所有匹配的子串放入数组中
- reresult = re.findall(regex, subject)
8.遍历所有匹配的子串
- (Iterate over all matches in a string)
- for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject)
- # match start: match.start()
- # match end (exclusive): match.end()
- # matched text: match.group()
9.通过Python正则表达式 字符串创建一个正则表达式对象
- (Create an object to use the same regex for many
operations)- rereobj = re.compile(regex)
10.用法1的Python正则表达式对象版本
- rereobj = re.compile(regex)
- if reobj.search(subject):
- do_something()
- else:
- do_anotherthing()
以上就是对Python正则表达式相关问题匹配的解决方案。
0 0
- Python学习笔记(十):Python正则表达式
- python学习笔记(四)正则表达式
- python学习笔记二(正则表达式)
- Python学习笔记(五)正则表达式
- Python之学习笔记(正则表达式)
- python正则表达式学习笔记(上)
- python学习笔记(五):正则表达式
- python学习笔记21(正则表达式)
- Python学习笔记57(正则表达式)
- Python 学习笔记 正则表达式
- 【Python学习笔记】正则表达式
- Python 学习笔记-正则表达式
- python正则表达式学习 笔记
- Python 正则表达式学习笔记
- Python学习笔记<正则表达式与python>
- Python学习笔记15:Python正则表达式
- Python基础(十)正则表达式
- Python 正则表达式之 补充学习笔记
- SICA手机pos机全新网络安全话题 支招防骗子方法
- HDOJ2006
- QT 4.7支持中文
- HD 2050 递推 // 折线分割平面
- warn_slowpath_common
- Python学习笔记(十):Python正则表达式
- js中的this基础
- 体育课上天上人间
- MEF程序设计指南八:部件生命周期(Parts Lifetime)托管
- hdoj.2025 查找最大元素 20140722
- poj 3687 两种写法,差别好大,估计没人按第二种写,就当练习邻接表了
- 论文摘抄 - FlumeJava
- 利用tcpdump抓取ios的tcp数据包
- 萨蒂夫拉撒地方卡死短发读书