我写了这个正则表达式:
p = re.compile(r'''
\[\[ #the first [[
[^:]*? #no :s are allowed
.*? #a bunch of chars
(
\| #either go until a |
|\]\] #or the last ]]
)
''', re.VERBOSE)
Run Code Online (Sandbox Code Playgroud)
我想用来re.findall获取某些字符串的所有匹配部分.我写了一些测试代码,但它给了我奇怪的结果.
这段代码
g = p.finditer(' [[Imae|Lol]] [[sdfef]]')
print g
for elem in g:
print elem.span()
print elem.group()
Run Code Online (Sandbox Code Playgroud)
给我这个输出:
(3, 10)
[[Imae|
(20, 29)
[[sdfef]]
Run Code Online (Sandbox Code Playgroud)
做得很完美吗?但是当我这样做时:
h = p.findall(' [[Imae|Lol]] [[sdfef]]')
for elem in h:
print elem
Run Code Online (Sandbox Code Playgroud)
输出是这样的:
|
]]
Run Code Online (Sandbox Code Playgroud)
为什么findall()打印出与finditer相同的结果?
Findall返回匹配组列表.正则表达式中的parantheses定义了一个可以认为你想要的组,但你不需要组. (?:...)是一个非捕获的paranthesis.将你的正则表达式改为:
'''
\[\[ #the first [[
[^:]*? #no :s are allowed
.*? #a bunch of chars
(?: #non-capturing group
\| #either go until a |
|\]\] #or the last ]]
)
'''
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3610 次 |
| 最近记录: |