emacs, regex, multi-lines

On Apr 30, 1:09 am, Sebastien LE MAGUER wrote:
> Hi,
> I wonder how to extract lines using a regexp. My file contains something
> like that :
> theq() :
> and I want to extract all lines before theq (here line X)
> does anyone have an idea ?

regex is very limited in extracting text that span multiple lines.

what you want can be done in emacs, but we need a bit more detail. For example, what pattern does the lines you want start? “All lines before theq” doesn't specify how it starts.

A better solution is to use search-forward-regexp to search the begin pattern, mark, then search-forward-regexp again to search for the ending pattern “theq() :”, then do search-backward-regexp to move point to the beginning of “theq”. Then, grab the region.

here's some pieces of code (untested):

(let (p1 p2)
(goto-char (point-min))
(search-forward-regexp "^A.+$") ; begin pattern
(setq p1 (point)) ; save cursor pos
(search-forward-regexp "theq() :") ; ending pattern
(backward-char 8)
(setq p2 (point)) ; save cursor pos
(setq mytext (buffer-substring p1 p2))

∑ http://xahlee.org/

No comments:

Post a Comment