tags:

views:

464

answers:

3

I reallly should know how to do this by now, but --

in emacs how do you do a query-replace-regexp that will match across multiple lines?

as a trivial example I'd want <p>\(.*?\)</p> to match

<p>foo
bar
</p>
A: 

In Perl Regular Expressions it would be using the s flag: /<p>(.*?)</p>/s
Not sure how to say it to Emacs.

Havenard
+7  A: 
M-x re-builder

is your friend. And it led me to this regular expression:

"<p>\\(.\\|\n\\)*</p>"

which is the string version of

<p>\(.\|^J\)*</p>         ;# where you enter ^J by C-q C-j

And that works for me when I do re-search-forward, but not when I do 'query-replace-regexp. Unsure why...

Now, when doing a 're-search-forward (aka C-u C-s), you can type M-% which will prompt you for a replacement (as of Emacs 22). So, you can use that to do your search and replace with the above regexp.

Note, the above regexp will match until the last </p> found in the buffer, which is probably not what you want, so use re-builder to build a regexp that comes closer to what you want. Obviously regular expressions can't count parenthesis, so you're on your own for that - depends on how robust a solution you want.

Trey Jackson
Are there info files for re-builder? I'm curious about how to use it.
seth
Not that I can find. The Emacs Wiki doesn't have much on it either. But it's pretty self-explanatory (isn't all of Emacs :). After entering `re-builder`, type `C-c C-h` and you'll get a listing of bindings including those that apply to `re-builder` which all begin with `C-c`.
Trey Jackson
Yah, I got that far. Was just looking for something a bit more in depth. Thanks!
seth
+2  A: 

Try character classes. As long as you're using only ASCII character set, you can use [[:ascii:]] instead of the dot. Using the longer [[:ascii:][:nonascii:]] ought to work for everything.

Boojum
awesome thanks dude
Neil Sarkar