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: Ted Zlatanov
Subject: Re: extract lines with regexp
Date: Fri, 01 May 2009 10:54:29 -0500
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.92 (gnu/linux)

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]