Skip to content
Generate strings from regular expressions, for use with Clojure test.check and spec
Branch: master
Clone or download
Latest commit 7bb1f6b Nov 10, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/miner allow backslash literals for {|} Nov 9, 2018
test/miner allow backslash literals for {|} Nov 9, 2018
.gitignore Initial commit Aug 25, 2016
LICENSE string generator from regex Aug 25, 2016 bump release Nov 9, 2018
project.clj allow backslash literals for {|} Nov 9, 2018


A Clojure library with a test.check generator that generates strings from regular expressions. Use miner.strgen/string-generator with test.check and clojure.spec.

The clojure.spec library is new in Clojure 1.9.



[com.velisco/strgen "0.1.8"]

strgen on Clojars


(require '[miner.strgen :as sg])
(require '[clojure.spec :as s])
(require '[clojure.test.check.generators :as gen])

(gen/sample (sg/string-generator #"[A-Z]{2,4}"))

;;=> ("ZOHX" "ZOXZ" "INX" "JO" "MRMZ" "TO" "PEHM" "YNK" "FJ" "JWH")

(s/def ::foobar (let [re #"fo+(bar)?"] 
                   (s/spec (s/and string? #(re-matches re %))
                           :gen  #(sg/string-generator re))))
(s/exercise ::foobar)

;;=> (["fo" "fo"] ["fobar" "fobar"] ["fo" "fo"] ["foo" "foo"] ["foooo" "foooo"] 
;;    ["fooo" "fooo"] ["fo" "fo"] ["foobar" "foobar"] ["fooooobar" "fooooobar"] 
;;    ["fooooobar" "fooooobar"])


Not all Java regular expressions are supported. The basics work, including: x* y? z+. [abc] [a-z] [^a] \n (a|b) \w \W \d \D \s \S x{N} x{N,} x{M,N}. ^x$ ignores the leading ^ and trailing $ as they generate no characters. (Capture groups) and \1 style back references are not supported. Character groups like [:alnum:] are not supported. All the other fancy flags, quantifiers, character classes, etc. are unsupported. In summary, if I couldn't use the regex feature without looking it up, I didn't support it. If people ask for something, I might work on it.

When generating X-or-more items for regular expressions such as #"x*" or #"y+", the generator limits the number of items to a reasonably small count. You can control this with an optional second arg or-more-limit (an integer, default 9) when calling string-generator.

Related Projects

If you need better support for Java regular expressions when generating Strings, you should consider using the test.chuck library which provides the string-from-regex generator.


Copyright © 2016-2018 Stephen E. Miner

Distributed under the Eclipse Public License, same as Clojure.

You can’t perform that action at this time.