views:

122

answers:

3

I have a mako template that looks something like this:

% if staff:
    <!-- begin staff -->
    ...
    <!-- end staff -->
% endif

That way if I pass the staff variable as being True, those comments should appear. I'm trying to test this by using a regular expression that looks like this:

re.search('<!-- begin staff -->.*<!-- end staff -->', text)

I've verified that the comments appear in the HTML output, but the regular expression doesn't match. I've even tried putting the comments (<!-- begin staff --> and <!-- end staff -->) through re.escape, but still no luck. What am I doing wrong?

Or is there a better way to run this test?

+6  A: 

By default . doesn't match newlines - you need to add the re.DOTALL option.

re.search('<!-- begin staff -->.*<!-- end staff -->', text, re.DOTALL)

If you have more than one staff section, you might also want to make the match ungreedy:

re.search('<!-- begin staff -->.*?<!-- end staff -->', text, re.DOTALL)
Greg
Thank you very much. I figured it was something stupid. :-)
Jason Baker
+2  A: 

Use an HTML Parser like HTMLParser instead. See Can you provide some examples of why it is hard to parse XML and HTML with a regex? for why.

Chas. Owens
Excellent point. For the purposes I'm using this for, a simple regex should suffice. If I need to do anything more complicated, this is good to keep in mind.
Jason Baker
The point is that a simple regex is never enough even for a simple looking case.
Chas. Owens
+1  A: 

The problem is the newlines. Check out this page for an explanation of how to match newlines in your regex.

RossFabricant