[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Simple Regexp help

Joe Blow

1/6/2009 11:51:00 PM

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}]/
--
Posted via http://www.ruby-....

6 Answers

Tim Greer

1/7/2009 12:18:00 AM

0

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. I.e., /^[a-z]+$/i will only
match a-z characters from start to end. You can then change the
character class to whatever you wish. ^ is the start of the string and
$ is the end of the string. + is one or more characters, so ^[a-z]+$ is
one of more characters in the character class [], being a-z, and noting
else.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Siep Korteling

1/7/2009 1:18:00 AM

0

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

Robert Klemme

1/7/2009 6:53:00 AM

0

On 07.01.2009 01:17, 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. I.e., /^[a-z]+$/i will only
> match a-z characters from start to end.

Note, this is not totally safe:

irb(main):001:0> s = "foo\n"
=> "foo\n"
irb(main):002:0> /^[a-z]+$/i =~ s
=> 0

As you can see, even a string with a newline at the end passes. This
version is safer because anchors do actually use start and end of the
string:

irb(main):003:0> /\A[a-z]+\z/i =~ s
=> nil

> You can then change the
> character class to whatever you wish. ^ is the start of the string and
> $ is the end of the string.

Actually ^ is a line start and $ is a line end (see above). For proper
anchoring string start and end you need \A and \z.

> + is one or more characters, so ^[a-z]+$ is
> one of more characters in the character class [], being a-z, and noting
> else.

Note also that, depending on definition of legal string the expression
should probably contain * instead of + because also the empty string
does not contain any illegal characters.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end

Joe Blow

1/7/2009 5:00:00 PM

0

Robert thanks \A and \z is what I was looking for.

Can you explain though what that does though? I can not seem to find any
relevant information on it.

--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

1/7/2009 5:30:00 PM

0

On Wed, Jan 7, 2009 at 5:59 PM, Joe Blow <zmanods@hotmail.com> wrote:
> Robert thanks \A and \z is what I was looking for.
>
> Can you explain though what that does though? I can not seem to find any
> relevant information on it.

\A matches the beggining of the string and \z matches the end of the string.
If you don't "anchor" the regexp with those, what you match can match
anywhere within the string, even a substring. So for example:

irb(main):001:0> a = "abcdef"
=> "abcdef"
irb(main):005:0> a =~ /bc/
=> 1

This means that the regexp /bc/ has matched the string at position
one. Notice now if you anchor it that it doesn't match:

irb(main):006:0> a =~ /\Abc\z/
=> nil
irb(main):007:0> a =~ /\Aabcdef\z/
=> 0

www.rubular.com has a regular expression editor, where you can test,
and also a quick reference guide and a link to an online copy of the
pickaxe, although to be honest, not much is said about \A and \z
specifically.

You can also search here for more info about regexps:

http://www.regular-express...

Hope this helps,

Jesus.

Tim Greer

1/7/2009 6:11:00 PM

0

Robert Klemme wrote:

> irb(main):001:0> s = "foo\n"
> => "foo\n"
> irb(main):002:0> /^[a-z]+$/i =~ s
> => 0
>

Not knowing exactly what the OP wanted, it was only a quick example, so
what is "safe" is relative. Anyway, the use of \A and \z is indeed
probably a better example regardless of not knowing exactly what they
want.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!