470,613 Members | 2,528 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,613 developers. It's quick & easy.

re.sub problem

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?

Mar 31 '06 #1
4 924
Actually, it happens in general when there is more than one linebreak
between the open and close statements; not only when there are empty
lines.

Mar 31 '06 #2
Okay I just woke up and haven't had enough coffee so if I'm off here
please forgive me. Are you saying that if there is an emptly line then
it borks? If so just use re.S ( re.DOTALL ) and that should take care
of it. It will treat the ( . ) special. Otherwise it ignores new
lines.

Mar 31 '06 #3
Thanks a lot! Compiling with re.DOTALL did fix my problem for the most
part; there still are a few problems with my code, but I think I can
fix those myself.

Again, thanks!
Okay I just woke up and haven't had enough coffee so if I'm off here
please forgive me. Are you saying that if there is an emptly line then
it borks? If so just use re.S ( re.DOTALL ) and that should take care
of it. It will treat the ( . ) special. Otherwise it ignores new
lines.


Mar 31 '06 #4
Glad I could help.

Mar 31 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

117 posts views Thread by Peter Olcott | last post: by
28 posts views Thread by Jon Davis | last post: by
6 posts views Thread by Ammar | last post: by
2 posts views Thread by Mike Collins | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.