1

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?

Jason Baker
  • 192,085
  • 135
  • 376
  • 510

2 Answers2

9

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
  • 316,276
  • 54
  • 369
  • 333
2

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.

Community
  • 1
  • 1
Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
  • 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 Apr 16 '09 at 17:28
  • The point is that a simple regex is never enough even for a simple looking case. – Chas. Owens Apr 16 '09 at 17:37