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>
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With