Python re.findall regex problems
Question: I'm trying to find some very specific data in a string. The problem is I'm not
finding all of the data with the current regex I'm using. Here is some sample
data:

    
    
    [img:2gcfa9cc]http&#58;//img823&#46;imageshack&#46;us/img823/3295/pokaijumonlogo&#46;jpg[/img:2gcfa9cc]
    
    Making these little guys into Kaiju monsters.  Again, I know nothing about them, other then which ones I thought would make for cool possible Kaiju (of the original 150) so here's Day 01
    
    [b:2gcfa9cc][size=150:2gcfa9cc]BULBASAUR[/size:2gcfa9cc][/b:2gcfa9cc]
    [i:2gcfa9cc]Feb 01[/i:2gcfa9cc]
    [ddf2k12:2gcfa9cc]http&#58;//img853&#46;imageshack&#46;us/img853/2185/dailydrawfeb2012day01&#46;jpg[/ddf2k12:2gcfa9cc]
    
    Setting myself up with the same &quot;parameters&quot; as last year
    
    I may be breaking my own Challenge rules right now but...well I started this last night and I couldn't just leave 'em out in the cold all unfinished 'n' shit.  
    
    Obligatory Skyrim drawing.
    
    [ddf2k12:2ytorpmj]http&#58;//4&#46;bp&#46;blogspot&#46;com/-UIUSNXvnHz4/TynYf1BZ9oI/AAAAAAAAAl4/pRLHVP0Ny3U/s1600/01_cheatingcheaterwarmup1&#46;jpg[/ddf2k12:2ytorpmj]
    

What I'm trying to get is the data between the `ddf2k12` tags and the `img`
tags. I've only worked on the `ddf2k12` tags thus far (I figure the latter
will be the former with img instead of `ddf2k12`) and out of the 1586 tags I
should have found, I'm only getting 5. Here's my regex:

    
    
    ddf2k12_regex = '(\[[ddf2k12]+\:[A-Za-z0-9]+\])(.*?)(\[[ddf2k12]+\:[A-Za-z0-9]+\])'
    ddf2k12_find = re.findall(ddf2k12_regex, post)
    

Obviously there's something wrong with my regex, but after banging my head
against a wall I can't sort it out, so any help is appreciated. Thanks.


Answer: You will do yourself a big favor by breaking down that big regex into parts
and use composition. This seems to work correctly, and it's more obvious how
to debug it.

    
    
    import re
    
    start_tag =    '(\[{tagname}:[^\]]+\])'
    end_tag = start_tag.replace('\[', '\[\/', 1)
    content = '((?:.|\n)*?)' # The ?: indicates a non-capturing group.                                                                                             
    tag = start_tag + content + end_tag
    
    ddf_tag=tag.format(tagname='ddf2k12')
    
    for match in re.findall(ddf_tag, post):
        print match
    

