2

Following is sample text.

Header
This is test, and
mid line adsf
bag a lot of many things
shoes>
shoes/
This line should not be affected.
Neither this line.
This is test, and
mid line
bag a lot of many things
shoes>
shoes/
Footer

Need to replace/remove lines where text starts with This is test and ends with

shoes>
shoes/

using Emacs 26.1

Newbie to RegEx (emacs), following were my trials, failed to select multiple instance.

(This is test).*(shoes/)
^This is test.*shoes/$

Expected output is

Header
This line should not be affected.
Neither this line.
Footer
4

You can use the following regexp

This is test\(.*\n\)+?shoes>\nshoes/\n
  • .*\n matches a line
  • +? matches multiple lines in non-greedy way

Using the regexp in C-M-s (isearch-forward-regexp):

enter image description here

Note: to enter \n in the minibuffer of Isearch, use C-q C-j. As you can see, Isearch renders it as something like ^J.

When the matching is expected, to delete the matched text, type M-% (isearch-query-replace) to switch to Query-Replace, and type RET to supply an empty replacement, then type ! to replace all matches. In the end, you will get:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.