Count the occurrences of a pattern in a string.
lispworks
count-regexp-occurrences pattern string &key start end overlap case-sensitive space-string => count
pattern⇩ |
A string or a precompiled-regexp. |
string⇩ |
A string. |
Bounding index designators of string. | |
overlap⇩ |
A generalized boolean. |
case-sensitive⇩ |
A generalized boolean. |
space-string⇩ |
count |
An integer. |
The function count-regexp-occurrences
counts the occurrences of pattern in the part of string bounded by start and end.
If pattern is a string, count-regexp-occurrences
precompiles it first. If you use count-regexp-occurrences
with the same pattern string several times, it is better to precompile it using precompile-regexp.
start and end have the sames meaning as in count and other Common Lisp sequence functions.
If overlap is false (the default), then count-regexp-occurrences
counts matches that to not overlap. If overlap is non-nil, matches can overlap, and count-regexp-occurrences
finds all of the ways in which the pattern can be matched inside string.
case-sensitive controls whether a string pattern is precompiled as a case sensitive or case insensitive search. A non-nil value means a case sensitive search. The value nil
(the default) means a case insensitive search.
When space-string is non-nil and pattern is a string, then a "Lax whitespace" search is performed. That means that any sequence of space characters in pattern is effectively replaced by the regexp specified by space-string. If space-string is t
, it specifies a regexp that matches "whitespace", specifically any non-empty sequence of the space, tab, return or newline characters.
The regular expression syntax used by count-regexp-occurrences
is similar to that used by Emacs, as described in 28.7 Regular expression syntax.
(count-regexp-occurrences "aaa" "aaaaa") => 1 (count-regexp-occurrences "aaa" "aaaaa" :overlap t) => 3 (count-regexp-occurrences "12" "81267124") => 2 (count-regexp-occurrences "12" "81267124" :start 4) => 1 (let* ((path (example-file "capi/elements/text-input-pane.lisp")) (file-string (file-string path))) (count-regexp-occurrences ":title" file-string)) => 20 ; in LispWorks 7.1
LispWorks® User Guide and Reference Manual - 01 Dec 2021 19:30:41