diff options
-rw-r--r-- | api.lisp | 19 | ||||
-rw-r--r-- | docs/index.html | 21 | ||||
-rw-r--r-- | packages.lisp | 1 | ||||
-rw-r--r-- | test/simple | 6 |
4 files changed, 47 insertions, 0 deletions
@@ -514,6 +514,25 @@ declarations." nil)))) ,@body)))))) +(defun count-matches (regex target-string + &key (start 0) + (end (length target-string))) + "Returns a count of all substrings of TARGET-STRING which match REGEX." + (declare #.*standard-optimize-settings*) + (let ((count 0)) + (do-matches (s e regex target-string count + :start start :end end) + (incf count)))) + +#-:cormanlisp +(define-compiler-macro count-matches (&whole form regex &rest rest) + "Make sure that constant forms are compiled into scanners at +compile time." + (cond ((constantp regex) + `(count-matches (load-time-value (create-scanner ,regex)) + ,@rest)) + (t form))) + (defun all-matches (regex target-string &key (start 0) (end (length target-string))) diff --git a/docs/index.html b/docs/index.html index 6da6a15..24b395a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -84,6 +84,7 @@ href="http://weitz.de/regex-coach/">The Regex Coach</a>. <li><a href="#do-matches"><code>do-matches</code></a> <li><a href="#do-matches-as-strings"><code>do-matches-as-strings</code></a> <li><a href="#do-register-groups"><code>do-register-groups</code></a> + <li><a href="#count-matches"><code>count-matches</code></a> <li><a href="#all-matches"><code>all-matches</code></a> <li><a href="#all-matches-as-strings"><code>all-matches-as-strings</code></a> </ol> @@ -747,6 +748,26 @@ NIL </blockquote> + + +<p><br>[Function] +<br><a class=none name="count-matches"><b>count-matches</b> <i>regex target-string <tt>&key</tt> start end</i> => <i>list</i></a> + +<blockquote><br> +Returns a count of all matches of <code><i>regex</i></code> against +<code><i>target-string</i></code>. + +<pre> +* (count-matches "a" "foo bar baz") +2 + +* (count-matches "\\w*" "foo bar baz") +6 +</pre></blockquote> + + + + <p><br>[Function] <br><a class=none name="all-matches"><b>all-matches</b> <i>regex target-string <tt>&key</tt> start end</i> => <i>list</i></a> diff --git a/packages.lisp b/packages.lisp index 74faa44..5c2a0e4 100644 --- a/packages.lisp +++ b/packages.lisp @@ -45,6 +45,7 @@ :do-scans :do-matches :do-matches-as-strings + :count-matches :all-matches :all-matches-as-strings :split diff --git a/test/simple b/test/simple index 2aa1446..da336c8 100644 --- a/test/simple +++ b/test/simple @@ -93,6 +93,12 @@ (equal (nreverse result) '(12 * 15 - 42 / 3))) +(equal (count-matches "a" "foo bar baz") + 2) + +(equal (count-matches "\\w*" "foo bar baz") + 6) + (equal (all-matches "a" "foo bar baz") (list 5 6 9 10)) |