From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Sebastien Le Maguer Newsgroups: gmane.emacs.help Subject: Re: extract lines with regexp Date: Fri, 01 May 2009 14:02:10 +0200 Message-ID: <49FAE4C2.8070509@irisa.fr> References: <91723ea9-de6a-4963-918d-b2d53e76b832@p6g2000pre.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1241179400 28069 80.91.229.12 (1 May 2009 12:03:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 1 May 2009 12:03:20 +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 14:03:10 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 1LzrSP-0006Mb-8t for geh-help-gnu-emacs@m.gmane.org; Fri, 01 May 2009 14:03:09 +0200 Original-Received: from localhost ([127.0.0.1]:55197 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LzrSO-0004v1-PW for geh-help-gnu-emacs@m.gmane.org; Fri, 01 May 2009 08:03:08 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LzrRh-0004tA-Ck for help-gnu-emacs@gnu.org; Fri, 01 May 2009 08:02:25 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LzrRc-0004pr-8c for help-gnu-emacs@gnu.org; Fri, 01 May 2009 08:02:24 -0400 Original-Received: from [199.232.76.173] (port=43425 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LzrRb-0004pW-Rr for help-gnu-emacs@gnu.org; Fri, 01 May 2009 08:02:19 -0400 Original-Received: from mail1-relais-roc.national.inria.fr ([192.134.164.82]:42393) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_ARCFOUR_SHA1:16) (Exim 4.60) (envelope-from ) id 1LzrRb-0002MG-6x for help-gnu-emacs@gnu.org; Fri, 01 May 2009 08:02:19 -0400 X-IronPort-AV: E=Sophos;i="4.38,431,1233529200"; d="scan'208";a="28596619" Original-Received: from arennes-252-1-75-37.w86-195.abo.wanadoo.fr (HELO [192.168.1.69]) ([86.195.162.37]) by mail1-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 01 May 2009 14:02:14 +0200 User-Agent: Thunderbird 2.0.0.21 (X11/20090409) In-Reply-To: <91723ea9-de6a-4963-918d-b2d53e76b832@p6g2000pre.googlegroups.com> X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. 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:64129 Archived-At: In fact I need just the line before theq (). All lines, except thoses which begin with "theq", respect the same topology : ../rep1/rep2/nom_ficXXX_refXX_sentX.wav I can use your code to build what what I want. I will send my solution when it will be finished Thanks a lot Xah Lee a écrit : > 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? “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/ > > ☄ >