From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Xah Lee Newsgroups: gmane.emacs.help Subject: Re: extract lines with regexp Date: Thu, 30 Apr 2009 11:57:06 -0700 (PDT) Organization: http://groups.google.com Message-ID: <91723ea9-de6a-4963-918d-b2d53e76b832@p6g2000pre.googlegroups.com> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1241150394 30404 80.91.229.12 (1 May 2009 03:59:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 1 May 2009 03:59:54 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri May 01 05:59:45 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 1Lzjua-0003wq-T6 for geh-help-gnu-emacs@m.gmane.org; Fri, 01 May 2009 05:59:45 +0200 Original-Received: from localhost ([127.0.0.1]:40927 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Lzjua-0003hf-3h for geh-help-gnu-emacs@m.gmane.org; Thu, 30 Apr 2009 23:59:44 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!news.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!p6g2000pre.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help,comp.emacs Original-Lines: 47 Original-NNTP-Posting-Host: 24.6.175.142 Original-X-Trace: posting.google.com 1241117827 10357 127.0.0.1 (30 Apr 2009 18:57:07 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Thu, 30 Apr 2009 18:57:07 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p6g2000pre.googlegroups.com; posting-host=24.6.175.142; posting-account=bRPKjQoAAACxZsR8_VPXCX27T2YcsyMA User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_4_11; en) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/3.2.1 Safari/525.27.1, gzip(gfe), gzip(gfe) Original-Xref: news.stanford.edu gnu.emacs.help:168851 comp.emacs:98122 X-Mailman-Approved-At: Thu, 30 Apr 2009 23:59:22 -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:64128 Archived-At: 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? =E2=80=9CAll lines bef= ore theq=E2=80=9D 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 =E2=80=9Ctheq() :=E2=80=9D, then do search-backward-regexp t= o move point to the beginning of =E2=80=9Ctheq=E2=80=9D. 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 =E2=88=91 http://xahlee.org/ =E2=98=84