[Top][All Lists]

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

RE: count regexp hits

From: Drew Adams
Subject: RE: count regexp hits
Date: Wed, 3 Jan 2018 21:02:02 -0800 (PST)

> Just wrote this [1] - is this somewhere already
> or is it so simple so you are expected to do it
> yourself? If so - well, don't count on it.
> I actually know many people incapable of
> writing any of this!
> (defun count-regexp-hits (regexp)
>   (interactive "sregexp: ")
>   (let ((hits 0))
>     (save-excursion
>       (while (re-search-forward regexp (point-max) t)
>         (cl-incf hits) ))
>     (message "%d" hits) ))

`C-h f count-matches':

count-matches is an alias for `how-many' in `replace.el'.

(count-matches REGEXP &optional RSTART REND INTERACTIVE)

Print and return number of matches for REGEXP following point.
When called from Lisp and INTERACTIVE is omitted or nil, just return
the number, do not print it; if INTERACTIVE is t, the function behaves
in all respects as if it had been called interactively.

If REGEXP contains upper case characters (excluding those preceded by `\')
and `search-upper-case' is non-nil, the matching is case-sensitive.

Second and third arg RSTART and REND specify the region to operate on.

Interactively, in Transient Mark mode when the mark is active, operate
on the contents of the region.  Otherwise, operate from point to the
end of (the accessible portion of) the buffer.

This function starts looking for the next match from the end of
the previous match.  Hence, it ignores matches that overlap
a previously found match.

reply via email to

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