From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Raymond Wiker Newsgroups: gmane.emacs.help Subject: Re: extract lines with regexp Date: Mon, 04 May 2009 19:44:59 +0200 Message-ID: References: <91723ea9-de6a-4963-918d-b2d53e76b832@p6g2000pre.googlegroups.com> <86skjoddje.fsf@lifelogs.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1241479751 30721 80.91.229.12 (4 May 2009 23:29:11 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 4 May 2009 23:29:11 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue May 05 01:29:01 2009 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1M17al-00053r-Ob for geh-help-gnu-emacs@m.gmane.org; Tue, 05 May 2009 01:29:00 +0200 Original-Received: from localhost ([127.0.0.1]:39831 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1M17al-0007ia-60 for geh-help-gnu-emacs@m.gmane.org; Mon, 04 May 2009 19:28:59 -0400 Original-Newsgroups: gnu.emacs.help,comp.emacs User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.5-b28 (darwin) Cancel-Lock: sha1:elgZcrs0ASCAqNP/JkLy0cRY4/g= Original-NNTP-Posting-Host: 161.84-48-40.nextgentel.com X-Original-NNTP-Posting-Host: 161.84-48-40.nextgentel.com Original-X-Trace: news.broadpark.no 1241459107 84.48.40.161 (4 May 2009 19:45:07 +0100) Original-Lines: 63 Original-Path: news.stanford.edu!headwall.stanford.edu!news.glorb.com!eweka.nl!hq-usenetpeers.eweka.nl!border1.nntp.ams.giganews.com!nntp.giganews.com!feeder.xsnews.nl!feed.xsnews.nl!border-1.ams.xsnews.nl!usenet.hanse.com!nntp.gblx.net!nntp3.phx1!news.broadpark.no!not-for-mail Original-Xref: news.stanford.edu gnu.emacs.help:168906 comp.emacs:98126 X-Mailman-Approved-At: Mon, 04 May 2009 19:28:28 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:64181 Archived-At: Ted Zlatanov writes: > On Thu, 30 Apr 2009 11:57:06 -0700 (PDT) Xah Lee 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" A slightly more elaborate version (not necessarily more correct, but with a few more functions to ponder :-) ;;; ------------------------------------------------- (defun collect-all-before (pattern) (interactive "sPattern: ") (let (ret) (while (re-search-forward pattern nil t) (save-excursion (if (zerop (forward-line -1)) (push (buffer-substring (point) (progn (forward-line 1) (point))) ret)))) (with-output-to-temp-buffer "*tmp*" (set-buffer "*tmp*") (dolist (elt (nreverse ret)) (insert elt))))) ;;; ------------------------------------------------- (push ...) and (dolist ...) are from a package that tries to make Emacs-Lisp a bit more like Common Lisp, and can be trivially replaced with native Emacs-Lisp constructs.