re.sub problem
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

re.sub problem

From: veracon <veracon@gmail.com>
Date: Fri Mar 31 2006 - 15:33:32 CEST

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?
Received on Sun Apr 30 21:46:35 2006