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: Sebastien Le Maguer
Subject: Re: extract lines with regexp
Date: Fri, 01 May 2009 23:53:40 +0200
User-agent: Thunderbird 2.0.0.21 (X11/20090409)

I was thinking about (forward-line -1) but your solution is simpler.


Peter > all lines I want to remove respect the same topology of the ones I want to keep :)


Thanks all

Sébastien


Ted Zlatanov a écrit :
On Thu, 30 Apr 2009 11:57:06 -0700 (PDT) Xah Lee <xahlee@gmail.com> wrote:
XL> (let (p1 p2)
XL>   (save-excursion
XL>     (goto-char (point-min))
XL>     (search-forward-regexp "^A.+$") ; begin pattern
XL>     (setq p1 (point)) ; save cursor pos
XL>     (search-forward-regexp "theq() :") ; ending pattern
XL>     (backward-char 8)
XL>     (setq p2 (point)) ; save cursor pos
XL>     (setq mytext (buffer-substring p1 p2))
XL>     )
XL>   )

I don't think your first patten is exactly what the OP needed.

You can use (forward-line -1) to move the point back to the previous
line, and (beginning-of-line -1) to move to the beginning of the
previous line.  Also, you don't need search-forward-regexp the second
time, just search-forward will work.  Plus, of course, (backward-char 8)
is just asking for trouble.

Anyhow, regular expressions can handle multiple lines just fine:

A
theq() :
non
B
theq() :

(save-excursion
  (goto-char (point-min))
  (while (re-search-forward "\\(.*\\)\ntheq() :" nil t)
    (message (match-string 1))))

will produce "A" and "B"

HTH
Ted





reply via email to

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