help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: I need help with a regular expression


From: J. David Boyd
Subject: Re: I need help with a regular expression
Date: Wed, 05 May 2010 11:51:03 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (cygwin)

Cecil Westerhof <Cecil@decebal.nl> writes:

> I have written some code to count the number of functions in a buffer.
> At the moment I use the following regular expression for this:
>     "^(defun "
>
> This works fine, but then the defun's have to be on the start of the
> line. This is the most logical, but it is better to be save as sorry.
> This is why I wanted to write a more robust regular expression. I was
> thinking about something like:
>     "^[^;]+(defun "
>
> But that does not work. It marks the following completely, instead of
> the three at its own:
>     (defun a () (message "a"))
>     (defun b () (message "b"))
>     (defun c () (message "c"))
>
> Why is this? And how can I make a regular expression that does what I
> want?

There's a book that explains this, sorry but I can't remember the name
of it, something to do, of course, with "Regular Expressions".

The problem is that the expression you gave it, is, as the author
explains, "hungry".

It tries to match as much as possible, not as least as possible.

In your case, it sees the '^' (start of line, then looks as far as
possible for the 'defun'.  

You did have a blank line after your last defun, right?  Otherwise, it
would have kept on going.

Go to O'Reilly, and hunt for books on regular expressions.  It was only
a few 100 pages, good price, and explained a great deal.

Good luck!





reply via email to

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