Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs query-replace-regexp multiline

Tags:

regex

emacs

How do you do a query-replace-regexp in Emacs that will match across multiple lines?

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

<p>foo
bar
</p>
like image 676
Neil Sarkar Avatar asked Aug 20 '09 21:08

Neil Sarkar


2 Answers

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.

like image 56
Trey Jackson Avatar answered Oct 17 '22 13:10

Trey Jackson


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.

like image 28
Boojum Avatar answered Oct 17 '22 11:10

Boojum