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 14:02:10 +0200
User-agent: Thunderbird 2.0.0.21 (X11/20090409)

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 <Sebastien.Le_mag...@irisa.fr>
wrote:
Hi,

I wonder how to extract lines using a regexp. My file contains something
like that :

<useless lines>
<line X>
theq() :
<useless lines>

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/





reply via email to

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