Generate random string matching specific pattern and length

K

Kevin

[Note: parts of this message were removed to make it a legal post.]

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.
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

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-]/.
 
7

7stud --

Adam Prescott wrote in post #997841:
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.
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

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.
 
A

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

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("")
 
R

Robert Klemme

I'm trying to generate a random set of strings to fill a database with th= at
match the following pattern: =A0/^([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-strin=
gs

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 =A0/^([A-Z]|[a-z]|-)+$/ =A0&
char.len=3D=3D10
=A0strings<< current_matching_value
}

would achieve the right solution. =A0Any pointers would be greatly
appreciated.

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

BASE =3D 'A'.getbyte 0
DELTA =3D 'Z'.getbyte(0) - BASE

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

puts s, s.length
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
K

Kevin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top