I wrote up this regular expression:
p = re.compile(r'''
\[\[ #the first [[
[^:]*? #no :s are allowed
.*? #a bunch of chars
(
\| #either go until a |
|\]\] #or the last ]]
)
''', re.VERBOSE)
I want to use re.findall to get all the matching sections of some string. I wrote some test code, but it gives me bizarre results.
This code
g = p.finditer(' [[Imae|Lol]] [[sdfef]]')
print g
for elem in g:
print elem.span()
print elem.group()
gives me this output:
(3, 10)
[[Imae|
(20, 29)
[[sdfef]]
Makes perfect sense right? But when I do this:
h = p.findall(' [[Imae|Lol]] [[sdfef]]')
for elem in h:
print elem
the output is this:
|
]]
Why isn’t findall() printing out the same results as finditer??
Findall returns a list of matching groups. The parantheses in your regex defines a group that findall thinks you want, but you don’t want groups.
(?:...)is a non-capturing paranthesis. Change your regex to: