12

I think it can be done by generating strings using brute force and then try to match them to the supplied regex and printing if match.

But is there a better way to do this?

Regex are used to test if a string matches a pattern. I am aware of that. I thought it would be interesting to do it the way around.

rahulroy9202
  • 2,730
  • 3
  • 32
  • 45
  • 1
    [Higher Order Perl has a section discussing this problem](http://books.google.com/books?id=4_q8JJWNaTsC&lpg=PA273&ots=tkMse5dbQy&dq=higher%20order%20perl%20generate%20strings%20from%20regex&pg=PA272#v=onepage&q=higher%20order%20perl%20generate%20strings%20from%20regex&f=false). It's worth reading for other reasons, too. – Dave Newton Feb 04 '14 at 14:40
  • 1
    What's the problem you're trying to solve, what's the question? – David Thomas Feb 04 '14 at 14:40
  • There's no built-in way to generate matching strings from a regular expression in JavaScript. You'd have to write code to do it, and there are definitely better ways to do it than to generate random strings and see whether they match. It's a non-trivial problem however. – Pointy Feb 04 '14 at 14:41
  • I wish to generate strings that match a regex. – rahulroy9202 Feb 04 '14 at 14:41
  • Can you provide an example? Do you want a method that, given a RegExp, will return x random Strings that match the pattern? – reergymerej Feb 04 '14 at 14:46
  • But why would you want to do that? – Jerry Feb 04 '14 at 14:46
  • @Pointy - I am looking for those better methods you mentioned sir. – rahulroy9202 Feb 04 '14 at 14:48
  • @blurd I wish to generate a string without hard-coding it. And I wan it to follow a pattern. And i wish to use regex. – rahulroy9202 Feb 04 '14 at 14:50
  • Regex is typically used to find strings that match a pattern, not to generate them. http://stackoverflow.com/questions/367586/generating-random-text-strings-of-a-given-pattern – reergymerej Feb 04 '14 at 14:51
  • @blurd I am aware of that. I thought it would be interesting to do it the way around. – rahulroy9202 Feb 04 '14 at 14:52
  • So what type of pattern do you need them to match, what's the regular expression they should match? – David Thomas Feb 04 '14 at 16:45
  • 2
    Possible duplicate of [Generate a random string based on a regular expression](http://stackoverflow.com/questions/8959850/generate-a-random-string-based-on-a-regular-expression) – Sjoerd Apr 12 '17 at 18:59

2 Answers2

14

If you're using JavaScript, there's Randexp which generates random strings that match a given regex.

Releases for browser

Philipp Gayret
  • 4,870
  • 1
  • 28
  • 34
  • 2
    Randexp generates only one random string from a regex. To generate every possible match, you can use [genexp.js](https://github.com/alixaxel/genex.js) instead. – Anderson Green Apr 07 '20 at 17:37
3

Use randexp.js, it does exactly what you want:

console.log(new RandExp(/^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}$/).gen());
console.log(new RandExp(/^[0-9]{4}$/).gen());
console.log(new RandExp(/^[0-9,A-Z]{4}$/).gen());
console.log(new RandExp(/^([A-Z]){5}([0-9]){4}([A-Z]){1}$/).gen());
<script src="https://github.com/fent/randexp.js/releases/download/v0.4.3/randexp.min.js"></script>
Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98