我写了这样的正则表达式:为什么re.findall()给我的结果不同于Python中的re.finditer()?
p = re.compile(r'''
\[\[ #the first [[
[^:]*? #no :s are allowed
.*? #a bunch of chars
(
\| #either go until a |
|\]\] #or the last ]]
)
''', re.VERBOSE)
我想用re.findall
得到一些字符串的所有匹配的部分。我写了一些测试代码,但它给了我奇怪的结果。
此代码
g = p.finditer(' [[Imae|Lol]] [[sdfef]]')
print g
for elem in g:
print elem.span()
print elem.group()
给了我这样的输出:
(3, 10)
[[Imae|
(20, 29)
[[sdfef]]
权非常有意义?但是,当我这样做:
h = p.findall(' [[Imae|Lol]] [[sdfef]]')
for elem in h:
print elem
输出是这样的:
|
]]
为什么不是的findAll()打印出相同的结果finditer?
这个问题解决了!我忘记了一点关于findall ...谢谢! – 2011-05-27 21:20:35