How to match multi-line regular expressions in Emacs Lisp

I was trying to fix the Emacs Template Toolkit mode file so that it correctly highlights multi-line directives. I could not find how to tell Emacs to make “.” match also a newline, but I found that the negative clause [^…] will match anything that is not specified, including newlines. So by using it with a rarely used character (such as a linefeed character, which is not commonly used in Unix) I can achieve what I wanted. The original regular expression was:

"\\(\\[%[-+]?\\)\\(.+?\\)\\([-+]?%\\]\\)"

and the new one is:

"\\(\\[%[-+]?\\)\\([^\r]+?\\)\\([-+]?%\\]\\)"

If someone knows if it is possible and how to enable multi-line regex matching in Emacs, I’d love to know!

Advertisements

5 responses to “How to match multi-line regular expressions in Emacs Lisp

  1. Ooh… thanks for attempting to fix this. It’s something that has been (pretty low down) on my TODO list for years.

    Please send me patches when you’re finished 🙂

  2. Hi Dave – wow! You were really quick in noticing my write up, I guess you do keep an eye on your server logs 😉

    My patch so far consists of replacing the regex as shown above. If I find a better way of doing it, I will let you know.

  3. How about using the null character, as in [^]]?

  4. The null character is backslash zero.

  5. Tom: that sounds useful. I will try it out. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s