help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: extract lines with regexp


From: Xah Lee
Subject: Re: extract lines with regexp
Date: Thu, 30 Apr 2009 11:57:06 -0700 (PDT)
User-agent: G2/1.0

On Apr 30, 1:09 am, Sebastien LE MAGUER <Sebastien.Le_mag...@irisa.fr>
wrote:
> Hi,
>
> I wonder how to extract lines using a regexp. My file contains something
> like that :
>
> <useless lines>
> <line X>
> theq() :
> <useless lines>
>
> 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)
  (save-excursion
    (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))
    )
  )

  Xah
∑ http://xahlee.org/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]