From: Siep Korteling Date: 2009-01-07T10:17:35+09:00 Subject: Re: Simple Regexp help Tim Greer wrote: > Joe Blow wrote: > >> How can I test a word to make sure it ONLY contains certain >> characters? >> >> Say i have an expression like /[A-Z]/i >> >> How could i have "Testing" pass but "Testing123" fail? >> >> PS. I can not dynamically create the expression so it looks like this >> /[A-Z{10}]/ > > If you only want a-z, set the character class to start and end the > string, otherwise it'll match anything with a character that's in the > alphabet, regardless of what follows it. (...) You can also test for the opposite, anything not in the range a-z. class String def all_letters? (self =~ /[^a-z]/i).nil? end end puts "Testing".all_letters? # => true puts "Tes34ting".all_letters? # => false hth, Siep -- Posted via http://www.ruby-forum.com/.