All Manuals > LispWorks® User Guide and Reference Manual > 38 The LISPWORKS Package

count-regexp-occurrences Function

Summary

Count the occurrences of a pattern in a string.

Package

lispworks

Signature

count-regexp-occurrences pattern string &key start end overlap case-sensitive space-string => count

Arguments
pattern
A string or a precompiled-regexp.
string
A string.
start, end
Bounding index designators of string.
overlap
A generalized boolean.
case-sensitive
A generalized boolean.
space-string
nil (the default), t or a regexp string.
Values
count
An integer.
Description

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.

Examples
(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
See also

find-regexp-in-string
precompile-regexp
precompiled-regexp


LispWorks® User Guide and Reference Manual - 01 Dec 2021 19:30:41