[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: non-greedy regexp
From: |
David Kastrup |
Subject: |
Re: non-greedy regexp |
Date: |
Mon, 23 Oct 2006 15:57:57 +0200 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) |
Robert Pontisso <rpontisso@yahoo.co.uk> writes:
> Hi. I've been having difficulty finding a non-greedy
> regular expression to match a single instance of an
> xml-type tag pair in a line. Given a line with
> several instances of <foo>...</foo>, is there a
> version of <foo>.*</foo> that won't match everything
> between the beginning of the first pair and closing of
> the last pair?
Well, <foo>.*?</foo> would be one possibility, but that does not cover
nested tags. You'd probably want something like
(let ((level 0))
(while (progn
(search-forward-regexp "<\\(/\\)?foo>")
(setq level (if (match-beginning 1) (1- level) (1+ level)))
(> 0 level))))
in order to match nested tags.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
- non-greedy regexp, Robert Pontisso, 2006/10/23
- Re: non-greedy regexp,
David Kastrup <=