re.sub problem

veracon veracon at gmail.com
Fri Mar 31 08:33:32 EST 2006


I'm trying to make a (tiny) template system (Cheetah and like have far
more than what I need), but I've run into a problem. To simplify
everything, I've decided to make for loops matching the indentation
level of the open and close statements; it appears to work fine, but
apparently it chokes once there are empty lines inside the string being
replaced in.

It's a bit hard to explain, so I'll just show an example:
        stm = re.compile('\n(\s+)\{\{for (.+?) in
(.+?)\}\}\n?(.+?)\n\\1\{\{end for\}\}', re.M)
        data = re.sub(stm, self.handle_for, data)

I do have a self.handle_for, and I can see that it's not called if I
give it the following string:
[... (not beginning of actual string) ]
  {{for baz in bar}}
  <p>foo:{baz}</p>
b

  {{end for}}

There, nothing is matched; if there wasn't an empty line, it would
match something.

What am I doing wrong?




More information about the Python-list mailing list