From: Robert Feldt Date: 2001-11-28T22:49:01+09:00 Subject: [ruby-talk:26784] Re: Anyone know of a Regexp pattern random string generator? On Wed, 28 Nov 2001, Ross Shaw wrote: > "Robert Feldt" wrote > > Rockit 0.4 can generate random strings fulfilling a grammar. The grammars > > can be non-regular so they include regexps. Will be released before > > christmas. > > > > So does this mean I could easily create my own Regexp#rand built on Rockit? > No, you'd have to parse the Regexp into a Rockit parser (it may sound strange but its true!). There will be functions to do this in Rockit but I can't guarantee it'll be in 0.4.0, maybe some time after that. > The sort of thing I want to see is > > re1 = /\d\d/ > re1.rand ==> "42" > re1.rand ==> "71" > > re2 = /ABC\d?\d/ > re2.rand ==> "ABC4" > re2.rand ==> "ABC39" > If you go ahead and write your own I'd be intersted in seeing it. Its not difficult once you have the individual pieces of the Regexp, but to get to them you'd have to parse it. There may be shortcuts but for the general case that's what you'll have to do. Btw, the above would look like: digit = symbols("0".."9") re1 = seq(digit, digit) re1.rand ==> "42" re1.rand ==> "71" re2 = seq(string("ABC"), maybe(digit), digit) re2.rand ==> "ABC4" in Rockit. Regards, Robert