Generate random string matching specific pattern and length

I’m trying to generate a random set of strings to fill a database with
that
match the following pattern: /^([A-Z]|[a-z]|-)+$/

The array I’m trying to create must contain strings that look like
“A12-34-4567” and be exactly ten characters long I have seen code on the
internet for generating random strings but I don’t have internet at home
at
the moment to really study examples like the one here:
http://coderrr.wordpress.com/2008/10/28/ruby-golf-generating-random-strings

I’m guessing that something like map { if /^([A-Z]|[a-z]|-)+$/ &
char.len==10
strings<< current_matching_value
}

would achieve the right solution. Any pointers would be greatly
appreciated.

On Tue, May 10, 2011 at 9:56 PM, Kevin [email protected] wrote:

I’m trying to generate a random set of strings to fill a database with that
match the following pattern: /^([A-Z]|[a-z]|-)+$/

The array I’m trying to create must contain strings that look like
“A12-34-4567” and be exactly ten characters long

How’s this?

characters = (“A”…“Z”).to_a + (“a”…“z”).to_a # there is a shortcut to
this
in 1.9, but it escapes me.
characters << “-”

10.times.map { a << b.sample }

Sure there’s a quicker way.

By the way, /[A-Z|[a-z]|-/ is equivalent to /[A-Za-z-]/.

Adam P. wrote in post #997841:

On Tue, May 10, 2011 at 9:56 PM, Kevin [email protected] wrote:

I’m trying to generate a random set of strings to fill a database with that
match the following pattern: /^([A-Z]|[a-z]|-)+$/

The array I’m trying to create must contain strings that look like
“A12-34-4567” and be exactly ten characters long

How’s this?

characters = (“A”…“Z”).to_a + (“a”…“z”).to_a # there is a shortcut to
this
in 1.9, but it escapes me.
characters << “-”

10.times.map { a << b.sample }

Sure there’s a quicker way.

By the way, /[A-Z|[a-z]|-/ is equivalent to /[A-Za-z-]/.

Of course with all your misnamed/non-existent variables, that code won’t
run. Atrocious.

On Tue, May 10, 2011 at 10:20 PM, 7stud – [email protected]
wrote:

Of course with all your misnamed/non-existent variables, that code won’t
run. Atrocious.

Thanks for that. I’d renamed them between checking it in irb and paste.
Badly, as you politely noted. It’s been a long day.

On Wed, May 11, 2011 at 05:56:02AM +0900, Kevin wrote:

char.len==10
strings<< current_matching_value
}

would achieve the right solution. Any pointers would be greatly
appreciated.

There’s probably something like a thousand ways to achieve that. By th
eway, it looks like you missed a character class – numbers. Your regex
should probably have been something like this:

/^([A-Z]|[a-z]|\d|-)+$/

In fact, if it should be exactly ten characters long, you might want
that
regex to look like this:

/^([A-Z]|[a-z]|\d|-){10}$/

If you want to create strings that contain random distributions of
characters from a list of possible characters, you could use an array:

foo = (
  ('A'..'Z').to_a << ('a'..'z').to_a << ('0'..'9').to_a << '-'
).flatten
bar = String.new
10.times { bar << foo.shuffle[0] }

Now . . . if you specifically want dashes at specific places in your
string (which I suspect you might), that changes things.

On Tue, May 10, 2011 at 10:12 PM, Adam P. [email protected]
wrote:

characters = (“A”…“Z”).to_a + (“a”…“z”).to_a # there is a shortcut to
this in 1.9, but it escapes me.
characters << “-”

10.times.map { a << b.sample }

To rectify this abomination, it comes from two ways:

a = []

this was the b:

characters = (“A”…“Z”).to_a + (“a”…“z”).to_a
characters << “-”

10.times { a << characters.sample }
a.join("")

or

characters = (“A”…“Z”).to_a + (“a”…“z”).to_a
characters << “-”

10.times.map { characters.sample }.join("")

On Wed, May 11, 2011 at 06:20:00AM +0900, 7stud – wrote:

Of course with all your misnamed/non-existent variables, that code
won’t run. Atrocious.

Whatever happened to “Matz is nice so we are nice”?

Maybe you could try replying with something a little more friendly like
“Your code contains a few errors.”

On Tue, May 10, 2011 at 10:56 PM, Kevin [email protected] wrote:

I’m trying to generate a random set of strings to fill a database with that
match the following pattern: /^([A-Z]|[a-z]|-)+$/

The array I’m trying to create must contain strings that look like
“A12-34-4567” and be exactly ten characters long I have seen code on the

That’s 11 characters btw.

internet for generating random strings but I don’t have internet at home at
the moment to really study examples like the one here:
http://coderrr.wordpress.com/2008/10/28/ruby-golf-generating-random-strings

Just a hint: that does not work on 1.9 because ?a does not return a
Fixnum any more.

I’m guessing that something like map { if /^([A-Z]|[a-z]|-)+$/ &
char.len==10
strings<< current_matching_value
}

would achieve the right solution. Any pointers would be greatly
appreciated.

Often there is not the right solution (-> TIMTOWTDI). So far nobody
seems to have suggested good old sprintf:

BASE = ‘A’.getbyte 0
DELTA = ‘Z’.getbyte(0) - BASE

10.times do
s = sprintf ‘%s%02d-%02d-%04d’,
(BASE + rand(DELTA)).chr,
rand(100),
rand(100),
rand(10000)

puts s, s.length
end

Kind regards

robert

On May 10, 4:56pm, Kevin [email protected] wrote:

I’m trying to generate a random set of strings to fill a database with that
match the following pattern: /^([A-Z]|[a-z]|-)+$/

This gem might fit your need: https://github.com/benburkert/randexp

cheers

Thank you everyone.

@Chad: Yes I do need the dashes in the string. I will probably
incorporate your other suggestion with respect to letting the regex
check the overall length as well

@Chris: That definitely looks like it might do the trick. While
writing my original message I had wondered if such a thing existed,
and what do you know, it did.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs