Question

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?

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top