Skip to content

Regexp#random_example added

Compare
Choose a tag to compare
@tom-lord tom-lord released this 08 Mar 13:37
· 227 commits to master since this release

You can now generate one, random, example string (from the set of ALL possible matches!!) for a given regular expression.

Previously, this could have been achieved by doing something like this:

/complicated \w{10} regex/.examples.sample(1)

However, this is horribly inefficient since you must store all 9765625 possible examples in memory before randomly choosing one!

With this new method, even extreme examples like this run relatively quickly:

/\w{1000}/.random_example

This is basically because only ONE example is ever stored in memory, no matter how complicated the regex!