第二个地球里面的图片findall里面的正则表达式是什么意思

retext = "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'
retext = "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'
retext = "JGood is a handsome boy, he is cool, clever, and so on..."print re.sub(r'\s+', '-', text)
import retext = "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'的单词用[]括起来。
http://www.python8.org/a/fenleiwenzhang/yuyanjichu/0.html
阅读(...) 评论()python正则表达式findall有关问题 - Perl/Python当前位置:& &&&python正则表达式findall有关问题python正则表达式findall有关问题&&网友分享于:&&浏览:0次python正则表达式findall问题
pattern&=&pile('(b/.*/e)',&re.I)
str1&=&'aaaaaaaab/ccccddddddddddd/efffffffffffb/ggggggggggg/essssssss'
print&(str1)
print&('**********************************************')
match1&=&pattern.findall(str1)
index&=&1;
for&match&in&match1:
#&delStr&=&match.group()
print&('%d'&%index&+&':&'&+&match)
index&+=&1
取得结果是:b/ccccddddddddddd/efffffffffffb/ggggggggggg/e,想取成b/ccccddddddddddd/e和b/ggggggggggg/e这样怎么写------解决思路----------------------import&re
pattern&=&pile(r'(b/[a-zA-Z]*/e)',&re.I)
str1&=&'aaaaaaaab/ccccddddddddddd/efffffffffffb/ggggggggggg/essssssss'
match1&=&pattern.findall(str1)
for&match&in&match1:
&&&&#&delStr&=&match.group()
&&&&print&match
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 1234567891011 Copyright & &&版权所有正则表达式的使用:findall,search,match,group等等,还有实例
Python的re模块有几个方法用来匹配正则表达式:
match & & search
& &findall
1.match()方法的工作方式是只有当被搜索字符串的开头匹配模式的时候它才能查找到匹配对象。
2.search()方法和match()类似,不过search()方法不会限制我们只从字符串的开头查找匹配
3.findall()方法会找到所有符合条件的匹配对象
match和search返回的是一个match对象,所以要向得到匹配对象要使用group方法取出,match对象.group()或者match对象.group(0)
& 表示所有匹配对象,初次之外如果正则表达式里有多个用括号包围的特殊部分,那么相应的可以按顺序用group(1),group(2)...取出,而且还可以命名别名,match对象还可以使用start()和end()方法。
& 详情参见: & &
findall()返回的是一个装有匹配对象的列表
举个例子:现在我要截图下面这张图片里面的阅读数然后传给item['readcount']
总共经历了两次提取:
extract取得的是个列表,用[0]取出元素内容,之后将编码为utf-8,
可以得到readcount为:"posted @
10:31 风的姿态 阅读(1315)
& & & readcount
p.xpath('div[@class="postDesc"]/text()').extract()[0].encode('utf-8')
使用search()得到的是match对象,所以要用group取出里面的内容,
同时正则表达式里括弧的前面的\是用来表示:这是括弧,最后得到的结果是:"阅读1342"
& & & readcount2
= re.search('阅读\(\d+\)', readcount).group()
上面得到的是:"阅读1342",所以还要提取一次
这一次直接提取出里面的数字,得到:"1342"
& & & readcount3
= re.search('\d+', readcount2).group()
&&item['readcount'] =
readcount3
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。6726人阅读
Python(62)
这四个方法是从某个字符串中寻找特定子串或判断某个字符串是否符合某个模式的常用方法。
re.match(pattern, string[, flags])从首字母开始开始匹配,string如果包含pattern子串,则匹配成功,返回Match对象,失败则返回None,若要完全匹配,pattern要以$结尾。
re.search(pattern, string[, flags])若string中包含pattern子串,则返回Match对象,否则返回None,注意,如果string中存在多个pattern子串,只返回第一个。
3、findall
re.findall(pattern, string[, flags])返回string中所有与pattern相匹配的全部字串,返回形式为数组。
4、finditer
re.finditer(pattern, string[, flags])返回string中所有与pattern相匹配的全部字串,返回形式为迭代器。
若匹配成功,match()/search()返回的是Match对象,finditer()返回的也是Match对象的迭代器,获取匹配结果需要调用Match对象的group()、groups或group(index)方法。
group()、groups()与group(index)的区别,如下所示:
&&& import re
&&& s = '23432werwre2342werwrew'
&&& p = r'(\d*)([a-zA-Z]*)'
&&& m = re.match(p,s)
&&& m.group()
'23432werwre'
&&& m.group(0)
'23432werwre'
&&& m.group(1)
'23432'
&&& m.group(2)
'werwre'
&&& m.groups()
('23432', 'werwre')
&&& m = re.findall(p,s)
[('23432', 'werwre'), ('2342', 'werwrew'), ('', '')]
&&& p=r'(\d+)'
&&& m=re.match(p,s)
&&& m.group()
'23432'
&&& m.group(0)
'23432'
&&& m.group(1)
'23432'
&&& m.groups()
('23432',)
&&& m=re.findall(p,s)
['23432', '2342']综上:
group():母串中与模式pattern匹配的子串;group(0):结果与group()一样;groups():所有group组成的一个元组,group(1)是与patttern中第一个group匹配成功的子串,group(2)是第二个,依次类推,如果index超了边界,抛出IndexError;findall():返回的就是所有groups的数组,就是group组成的元组的数组,母串中的这一撮组成一个元组,那一措组成一个元组,这些元组共同构成一个list,就是findall()的返回结果。另,如果groups是只有一个元素的元组,findall的返回结果是子串的list,而不是元组的list了。
s =&&用正则表达式把s分为, 6, 777&&& import re
&&& s=''
&&& m = re.findall(r'(\d)\1*',s)
&&& print m
['1', '3', '4', '6', '7']
&&& m = re.search(r'(\d)\*',s)
&&& m.group()
&&& m=re.search(r'(\d)\1*',s)
&&& m.group()
'111'
&&& m.groups()
('1',)
&&& m.group(0)
'111'
&&& m.group(1)
'1'
&&& m.group(2)
Traceback (most recent call last):
File &&stdin&&, line 1, in &module&
IndexError: no such group
&&& m=re.finditer(r'(\d)\1*',s)
&&& m.next().group()
'111'
&&& m.next().group()
'3'
&&& m.next().group()
'44'
&&& m.next().group()
'6'
&&& m.next().group()
'777'
&&& m.next().group()
Traceback (most recent call last):
File &&stdin&&, line 1, in &module&
StopIteration
另一个例子:
&&& p = r'(\d)\1+([a-zA-Z]+)'
&&& s = '1111werwrw3333rertert4444'
&&& p = r'(\d)\1+([a-zA-Z]*)'
&&& import re
&&& re.findall(p,s)
[('1', 'werwrw'), ('3', 'rertert'), ('4', '')]
&&& m = re.search(p,s)
&&& m.group()
'1111werwrw'
&&& m.group(1)
'1'
&&& m.group(2)
'werwrw'
&&& m.groups()
('1', 'werwrw')
&&& m = re.finditer(p,s)
&&& m.next().group()
'1111werwrw'
&&& m.next().group()
'3333rertert'
&&& m.next().group()
'4444'
&&& m.next().group()
Traceback (most recent call last):
File &&stdin&&, line 1, in &module&
StopIteration
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:95325次
积分:2028
积分:2028
排名:第16428名
原创:90篇
转载:100篇
评论:11条
(3)(14)(10)(12)(16)(4)(3)(12)(2)(11)(10)(8)(6)(17)(9)(6)(8)(1)(14)(6)(17)(9)(1)}

我要回帖

更多关于 正则表达式 取第二个 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信