Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Very simple regex question

Reply
Thread Tools

Very simple regex question

 
 
Geometric Patterns
Guest
Posts: n/a
 
      11-13-2010
Hi, I have a very simple regex question.

I don't understand why the regex in the example below isn't identifying
that the string is longer than 12 characters long...

>> line = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

=> "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
>> if line =~ /[a-zA-Z]{2,12}/
>> puts "good"
>> else

?> puts "bad"
>> end

good
=> nil
>>


Any ideas?

Many thanks in advance!

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

 
Reply With Quote
 
 
 
 
Jeremy Bopp
Guest
Posts: n/a
 
      11-13-2010
On 11/13/2010 01:06 PM, Geometric Patterns wrote:
> Hi, I have a very simple regex question.
>
> I don't understand why the regex in the example below isn't identifying
> that the string is longer than 12 characters long...
>
>>> line = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

> => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
>>> if line =~ /[a-zA-Z]{2,12}/
>>> puts "good"
>>> else

> ?> puts "bad"
>>> end

> good
> => nil
>>>

>
> Any ideas?


Your regexp will match anywhere within the string where there is a run
of 2 to 12 characters. If you want to ensure that there is nothing
before or after whatever is matched, you need to use the begin and end
of string anchors as follows when you define your regexp:

/\A[a-zA-Z]{2,12}\z/

-Jeremy

 
Reply With Quote
 
 
 
 
Hassan Schroeder
Guest
Posts: n/a
 
      11-13-2010
On Sat, Nov 13, 2010 at 11:06 AM, Geometric Patterns
<(E-Mail Removed)> wrote:

> I don't understand why the regex in the example below isn't identifying
> that the string is longer than 12 characters long...


>>> if line =~ /[a-zA-Z]{2,12}/


That succeeds if the string *contains* between 2 and 12 characters,
not if it consists solely of 2-12 characters.

line = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
if line =~ /[a-zA-Z]{2,12}/
puts "good" # you'll get this
else
puts "bad"
end

if line =~ /\A[a-zA-Z]{2,12}\Z/
puts "good"
else
puts "bad" # you'll get this
end

FWIW,
--
Hassan Schroeder ------------------------ http://www.velocityreviews.com/forums/(E-Mail Removed)
twitter: @hassan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Very simple hash/regex question Tuxedo Perl Misc 14 08-27-2012 06:19 AM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Help running a very very very simple code olivier.melcher Java 8 05-12-2008 07:51 PM
Very very very basic question Peter C Programming 14 02-14-2005 09:46 AM
very Very VERY dumb Question About The new Set( ) 's Raymond Arthur St. Marie II of III Python 4 07-27-2003 12:09 AM



Advertisments