I was emailing with John Gruber about his SmartyPants plugin. It wasn’t correctly parsing a file and we were trying to figure out the right preg expressions to match HTML comments, PHP, etc.
I learned quite a bit from our conversations—specifically about lazy quantifier. Perl operators match greedily (they try to find the biggest possible match), but adding ? to the operator will match lazily (finds the shortest possible match):
# Cumbersome way of matching tags $stuff =~ /<[^>]*>/; # Lazy way $stuff =~ /<.*?>/; # Also works for matching PHP code: $stuff =~ /<\?.*?\?>/;
I also got reminded that /s will match across lines:
# Match multiple lines the hard way $stuff =~ /(.|[\r\n])*/; # Match mutlitple lines the easy way $stuff =~ /.*/s;