Random Text generator based on regex [duplicate]
I would like to know if there is software that, given a regex and of course some other constraints like length, produces random text that always matches the given regex. Thanks
Solution 1:
Xeger is capable of doing it:
String regex = "[ab]{4,6}c";
Xeger generator = new Xeger(regex);
String result = generator.generate();
assert result.matches(regex);
Solution 2:
Yes, there exists software that can generate a random match to a regex:
- 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#
- regexp-examples, Ruby
- genex.js, JavaScript
- genex, Go
Solution 3:
All regular expressions can be expressed as context free grammars. And there is a nice algorithm already worked out for producing random sentences, from any CFG, of a given length. So upconvert the regex to a cfg, apply the algorithm, and wham, you're done.
Solution 4:
If you want a Javascript solution, try randexp.js.