Generating random strings from a regular expression
For testing purposes it is sometimes useful to create random strings that conform to some specified pattern. For example, you want to create random phone numbers that pass through the validation in your software. One easy way to specify the pattern is to use a regular expression or regex. For our phone numbers, we could specify `555-\d{4]`, for example.
Generating a random string from a regex is not easy to code yourself, but luckily somebody else has already wrote a program that does this. In fact, there are quite a number of tools available that can provide examples for any regular expression. Some of these can even list all possible matches to a regex.
These tools or libraries can create example matches for regular expression patterns:
- Exrex, Python
- Pxeger, Javascript
- regex-genex, Haskell
- Xeger, Java
- Xeger, Python
- Generex, Java
- rxrdg, C#
- String::Random, Perl
- regldg, C
- paggern, PHP
- ReverseRegex, PHP
- randexp.js, Javascript
- EGRET, Python/C++
- MutRex, Java
- Fare, C#
- rstr, Python
- randexp, Ruby
- goregen, Go
- bfgex, Java
- regexgen, Javascript
- strgen, Python
- random-string, Java
- regexp-unfolder, Clojure
- string-random, Haskell
- rxrdg, C#
- Regexp::Genex, Perl
- StringGenerator, Python
- strrand, Go
- regen, Go
- Rex, C#
And here are some recommendations if you don't know which one to choose:
- Online: randexp.js demo
- Windows command line: download rex.exe
- Linux command line: exrex
I hope this list can keep people from creating another regex library.