re.sub problem

V

veracon

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?
 
V

veracon

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.
 
R

RunLevelZero

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.
 
V

veracon

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top