转载:Python正则表达式的用法(1. 匹配)
2007-06-27 11:55
1216 查看
http://www.cnblogs.com/agateriver/archive/2005/08/29/225498.html
Agate River's Home
Focus on Python, QT, XeTeX and others.
首页 新随笔 联系 聚合
管理
posts - 12, comments - 4, trackbacks - 0
Python正则表达式的用法(1. 匹配)
下面列出Python正则表达式的几种匹配用法:
1.测试正则表达式是否匹配字符串的全部或部分
1
regex=ur"
" #正则表达式
2
if re.search(regex, subject):
3
do_something()
4
else:
5
do_anotherthing()
6
2.测试正则表达式是否匹配整个字符串
1
regex=ur"
/Z" #正则表达式末尾以/Z结束
2
if re.match(regex, subject):
3
do_something()
4
else:
5
do_anotherthing()
6
3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)
1
regex=ur"
" #正则表达式
2
match = re.search(regex, subject)
3
if match:
4
# match start: match.start()
5
# match end (exclusive): match.end()
6
# matched text: match.group()
7
do_something()
8
else:
9
do_anotherthing()
4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex)
1
regex=ur"
" #正则表达式
2
match = re.search(regex, subject)
3
if match:
4
result = match.group()
5
else:
6
result = ""
5. 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group)
1
regex=ur"
" #正则表达式
2
match = re.search(regex, subject)
3
if match:
4
result = match.group(1)
5
else:
6
result = ""
6. 获取有名组所匹配的子串(Get the part of a string matched by a named group)
1
regex=ur"
" #正则表达式
2
match = re.search(regex, subject)
3
if match:
4
result = match.group("groupname")
5
else:
6
result = ""
7. 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string)
1
result = re.findall(regex, subject)
8.遍历所有匹配的子串(Iterate over all matches in a string)
1
for match in re.finditer(r"<(.*?)/s*.*?//1>", subject)
2
# match start: match.start()
3
# match end (exclusive): match.end()
4
# matched text: match.group()
9.通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations)
1
reobj = re.compile(regex)
10.用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched)
1
reobj = re.compile(regex)
2
if reobj.search(subject):
3
do_something()
4
else:
5
do_anotherthing()
11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely)
1
reobj = re.compile(r"
/Z") #正则表达式末尾以/Z 结束
2
if reobj.match(subject):
3
do_something()
4
else:
5
do_anotherthing()
6
12.创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches (part of) a string)
1
reobj = re.compile(regex)
2
match = reobj.search(subject)
3
if match:
4
# match start: match.start()
5
# match end (exclusive): match.end()
6
# matched text: match.group()
7
do_something()
8
else:
9
do_anotherthing()
13.用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex)
1
reobj = re.compile(regex)
2
match = reobj.search(subject)
3
if match:
4
result = match.group()
5
else:
6
result = ""
14.用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group)
1
reobj = re.compile(regex)
2
match = reobj.search(subject)
3
if match:
4
result = match.group(1)
5
else:
6
result = ""
15.用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group)
1
reobj = re.compile(regex)
2
match = reobj.search(subject)
3
if match:
4
result = match.group("groupname")
5
else:
6
result = ""
16.用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string)
1
reobj = re.compile(regex)
2
result = reobj.findall(subject)
17.通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string)
1
reobj = re.compile(regex)
2
for match in reobj.finditer(subject):
3
# match start: match.start()
4
# match end (exclusive): match.end()
5
# matched text: match.group()
posted on 2005-08-29 18:46 玛瑙河 阅读(167) 评论(0) 编辑 收藏 引用 网摘 所属分类: Python
刷新评论列表
<script type="text/javascript"><!--
google_ad_client = "pub-4210569241504288";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text";
google_ad_channel ="";
google_color_link = "6699CC";
//--></script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script>
<iframe width="468" scrolling="no" height="60" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4210569241504288&dt=1182915717046&lmt=1182915716&format=468x60_as&output=html&correlator=1182915717031&url=http%3A%2F%2Fwww.cnblogs.com%2Fagateriver%2Farchive%2F2005%2F08%2F29%2F225498.html&color_link=6699CC&ad_type=text&ref=http%3A%2F%2Fwww.cnblogs.com%2Fagateriver%2Fcategory%2F68961.html&cc=20&flash=9&u_h=1024&u_w=1280&u_ah=957&u_aw=1280&u_cd=32&u_tz=480&u_his=2&u_java=true&u_nplug=30&u_nmime=151" name="google_ads_frame"></iframe>
Copyright ©2007 玛瑙河 Powered By: 博客园 模板提供:沪江博客
我的评论
我参与的随笔
查看公开留言
查看私人留言
XeTeX(3)
旅途(1)
闲看三国
杂记(3)
2006年9月 (1)
2006年4月 (2)
2006年2月 (1)
2005年9月 (1)
2005年8月 (4)
虎跳峡
丽江
香格里拉—梅里
博客园电子期刊
2. 使verbatim环境在XeTeX下支持中文
3. XeTeX下制作beamer演示文稿初体验
4. 用Python编写自动下载网络小说的脚本
5. Python & ADO
6. I Belive I Can Fly Lyrics
7. 幸福在哪里
8. 心的独旅
9. About my photo!
10. Python正则表达式的用法(2. 替换;3.拆分)
<script language="JavaScript" type="text/javascript">
function SearchGoogle(key,evt,site)
{
if(evt.keyCode==13 || evt.keyCode==0 || evt.type =='click')
{
key.focus();
var keystr = encodeURIComponent(key.value);
url = "http://www.google.com/search?q=";
url = url+keystr;
url += "&ie=UTF-8&oe=GB2312&hl=zh-CN&domains="+site+"&sitesearch="+site;
window.location=url;
return false;
}
}
</script>
最新评论
1. re: 开张志喜!
白洋
--玛瑙河
2. re: 开张志喜!
恭喜!
安福寺人?
--田勇志
3. re: 开张志喜!
感谢光临!老家枝江!
--玛瑙河
4. re: 开张志喜!
评论内容较长,点击标题查看
--syrinx
Agate River's Home
Focus on Python, QT, XeTeX and others.
首页 新随笔 联系 聚合
管理
posts - 12, comments - 4, trackbacks - 0
Python正则表达式的用法(1. 匹配)
下面列出Python正则表达式的几种匹配用法:
1.测试正则表达式是否匹配字符串的全部或部分
1
regex=ur"
" #正则表达式
2
if re.search(regex, subject):
3
do_something()
4
else:
5
do_anotherthing()
6
2.测试正则表达式是否匹配整个字符串
1
regex=ur"
/Z" #正则表达式末尾以/Z结束
2
if re.match(regex, subject):
3
do_something()
4
else:
5
do_anotherthing()
6
3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)
1
regex=ur"
" #正则表达式
2
match = re.search(regex, subject)
3
if match:
4
# match start: match.start()
5
# match end (exclusive): match.end()
6
# matched text: match.group()
7
do_something()
8
else:
9
do_anotherthing()
4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex)
1
regex=ur"
" #正则表达式
2
match = re.search(regex, subject)
3
if match:
4
result = match.group()
5
else:
6
result = ""
5. 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group)
1
regex=ur"
" #正则表达式
2
match = re.search(regex, subject)
3
if match:
4
result = match.group(1)
5
else:
6
result = ""
6. 获取有名组所匹配的子串(Get the part of a string matched by a named group)
1
regex=ur"
" #正则表达式
2
match = re.search(regex, subject)
3
if match:
4
result = match.group("groupname")
5
else:
6
result = ""
7. 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string)
1
result = re.findall(regex, subject)
8.遍历所有匹配的子串(Iterate over all matches in a string)
1
for match in re.finditer(r"<(.*?)/s*.*?//1>", subject)
2
# match start: match.start()
3
# match end (exclusive): match.end()
4
# matched text: match.group()
9.通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations)
1
reobj = re.compile(regex)
10.用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched)
1
reobj = re.compile(regex)
2
if reobj.search(subject):
3
do_something()
4
else:
5
do_anotherthing()
11.用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely)
1
reobj = re.compile(r"
/Z") #正则表达式末尾以/Z 结束
2
if reobj.match(subject):
3
do_something()
4
else:
5
do_anotherthing()
6
12.创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches (part of) a string)
1
reobj = re.compile(regex)
2
match = reobj.search(subject)
3
if match:
4
# match start: match.start()
5
# match end (exclusive): match.end()
6
# matched text: match.group()
7
do_something()
8
else:
9
do_anotherthing()
13.用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex)
1
reobj = re.compile(regex)
2
match = reobj.search(subject)
3
if match:
4
result = match.group()
5
else:
6
result = ""
14.用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group)
1
reobj = re.compile(regex)
2
match = reobj.search(subject)
3
if match:
4
result = match.group(1)
5
else:
6
result = ""
15.用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group)
1
reobj = re.compile(regex)
2
match = reobj.search(subject)
3
if match:
4
result = match.group("groupname")
5
else:
6
result = ""
16.用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string)
1
reobj = re.compile(regex)
2
result = reobj.findall(subject)
17.通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string)
1
reobj = re.compile(regex)
2
for match in reobj.finditer(subject):
3
# match start: match.start()
4
# match end (exclusive): match.end()
5
# matched text: match.group()
posted on 2005-08-29 18:46 玛瑙河 阅读(167) 评论(0) 编辑 收藏 引用 网摘 所属分类: Python
刷新评论列表
<script type="text/javascript"><!--
google_ad_client = "pub-4210569241504288";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text";
google_ad_channel ="";
google_color_link = "6699CC";
//--></script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script>
<iframe width="468" scrolling="no" height="60" frameborder="0" allowtransparency="true" hspace="0" vspace="0" marginheight="0" marginwidth="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4210569241504288&dt=1182915717046&lmt=1182915716&format=468x60_as&output=html&correlator=1182915717031&url=http%3A%2F%2Fwww.cnblogs.com%2Fagateriver%2Farchive%2F2005%2F08%2F29%2F225498.html&color_link=6699CC&ad_type=text&ref=http%3A%2F%2Fwww.cnblogs.com%2Fagateriver%2Fcategory%2F68961.html&cc=20&flash=9&u_h=1024&u_w=1280&u_ah=957&u_aw=1280&u_cd=32&u_tz=480&u_his=2&u_java=true&u_nplug=30&u_nmime=151" name="google_ads_frame"></iframe>
标题 | 请输入标题 | |
姓名 | 请输入你的姓名 | |
主页 | ||
内容(请不要发表任何与政治相关的内容) 请输入评论内容 | ||
登录后才能发表评论 | ||
Remember Me? | ||
登录 使用高级评论 新用户注册 返回页首 恢复上次提交 | ||
[使用Ctrl+Enter键可以直接提交] | ||
相关文章: 用Python编写自动下载网络小说的脚本 Python & ADO Python正则表达式的用法(2. 替换;3.拆分) Python正则表达式的用法(1. 匹配) | ||
常用链接
我的随笔我的评论
我参与的随笔
留言簿(1)
给我留言查看公开留言
查看私人留言
随笔分类
Python(4)XeTeX(3)
旅途(1)
闲看三国
杂记(3)
随笔档案
2007年4月 (3)2006年9月 (1)
2006年4月 (2)
2006年2月 (1)
2005年9月 (1)
2005年8月 (4)
相册
鄙人虎跳峡
丽江
香格里拉—梅里
友情链接
limodou的blog博客园电子期刊
最新随笔
1. 中文首行缩进的设置2. 使verbatim环境在XeTeX下支持中文
3. XeTeX下制作beamer演示文稿初体验
4. 用Python编写自动下载网络小说的脚本
5. Python & ADO
6. I Belive I Can Fly Lyrics
7. 幸福在哪里
8. 心的独旅
9. About my photo!
10. Python正则表达式的用法(2. 替换;3.拆分)
<script language="JavaScript" type="text/javascript">
function SearchGoogle(key,evt,site)
{
if(evt.keyCode==13 || evt.keyCode==0 || evt.type =='click')
{
key.focus();
var keystr = encodeURIComponent(key.value);
url = "http://www.google.com/search?q=";
url = url+keystr;
url += "&ie=UTF-8&oe=GB2312&hl=zh-CN&domains="+site+"&sitesearch="+site;
window.location=url;
return false;
}
}
</script>
搜索
最新评论
1. re: 开张志喜!白洋
--玛瑙河
2. re: 开张志喜!
恭喜!
安福寺人?
--田勇志
3. re: 开张志喜!
感谢光临!老家枝江!
--玛瑙河
4. re: 开张志喜!
评论内容较长,点击标题查看
--syrinx
相关文章推荐
- Python正则表达式的用法(1. 匹配)
- Python正则表达式的几种匹配用法
- Python正则表达式匹配中文用法示例
- 转载:Python正则表达式的用法(2. 替换;3.拆分)
- python 正则表达式匹配次数 findall 用法
- Python正则表达式的常用匹配用法
- Python正则表达式的几种匹配用法
- python非贪婪、多行匹配正则表达式例子[转载]
- Python正则表达式的几种匹配用法
- Python正则表达式的用法(1. 匹配)
- [转载]强大的grep用法详解:grep与正则表达式
- python正则表达式匹配时间和IP地址
- js正则表达式最长匹配(贪婪匹配)和最短匹配(懒惰匹配)用法分析
- Python正则表达式---全部能匹配的子串迭代器finditer及findall及以中文匹配部分中文
- Python正则表达式返回首次匹配到的字符及查询的健壮性
- Python中正则表达式对中文的匹配问题
- python 正则表达式匹配字符串末尾的错误
- [Python]解决正则表达式的"贪婪"匹配
- (学习)python非贪婪、多行匹配正则表达式例子
- Python使用中文正则表达式匹配指定中文字符串的方法示例