1

Is there a way of generating a string that would match a regular expression in Ruby

e.g.

regex           output
^\d{6}$         875914
^\d{3}-\w{2}    584-AS
Alan Moore
  • 73,866
  • 12
  • 100
  • 156

2 Answers2

2

From a response on a related SO question - Matt Aimonetti's randexp might be of some use. Not generalised to all regexp though...

EDIT: This version looks more up-to-date.

Community
  • 1
  • 1
martin clayton
  • 76,436
  • 32
  • 213
  • 198
1

I haven't tested it, but this looks like something you're looking for: http://code.google.com/p/xeger/

Note that I cannot believe it will handle more complex regex-es (using look-arounds, back-references, etc.). But again, I haven't tested it.

EDIT: sorry, I now just saw you're looking for a Ruby implementation...

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • 1
    That is... interesting. Poorly documented though, and the 2 tests they have there don't reveal much... – Kobi Dec 31 '09 at 10:48