Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regular Expresion Error?

Reply
Thread Tools

Regular Expresion Error?

 
 
Filipe
Guest
Posts: n/a
 
      09-29-2007
Hello all,

According to the Regular Expressions section of the following ruby
guide: http://www.ruby-doc.org/docs/Program..._stdtypes.html,
the piece of code bellow should have returned "He<<ll>>o", instead of
"No match". Has anyone got an idea on why it isn't working?

Notice that if I tell the interpreter how big \w should be in an
explicit way by adding {1,1}, the expected result is given.

def showRE(a,re)
#re = Regexp.new(re) if re.class==String
#re=Regexp.new(re.to_s) if re.class==Integer
if a=~re
"#{$`}<<#{$&}>>#{$'}"
else
"no match"
end
end

puts showRE("Hello", /(\w)\1/) #=> no match
puts showRE("Hello", /(\w{1,1})\1/) #=> He<<ll>>o

Thanks in advance for helping

 
Reply With Quote
 
 
 
 
Filipe
Guest
Posts: n/a
 
      09-29-2007
Forgot to mention: Im running ruby 1.8.6 at Mac OS X 10.4.

 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      09-30-2007
Filipe wrote:
> def showRE(a,re)
> #re = Regexp.new(re) if re.class==String
> #re=Regexp.new(re.to_s) if re.class==Integer
> if a=~re
> "#{$`}<<#{$&}>>#{$'}"
> else
> "no match"
> end
> end
>
> puts showRE("Hello", /(\w)\1/) #=> no match
> puts showRE("Hello", /(\w{1,1})\1/) #=> He<<ll>>o


Both return "He<<ll>>o" for me.
ruby 1.8.6 (2007-09-23 patchlevel 110) [x86_64-linux]


--
Jabber:
ICQ: 205544826

 
Reply With Quote
 
Wolfgang Nádasi-Donner
Guest
Posts: n/a
 
      09-30-2007
Filipe wrote:
> puts showRE("Hello", /(\w)\1/) #=> no match
> puts showRE("Hello", /(\w{1,1})\1/) #=> He<<ll>>o


Both return "He<<ll>>o" on my machine too (Windows2000 -
OneClickInstaller 186-25).

Wolfgang Nádasi-Donner
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Filipe
Guest
Posts: n/a
 
      10-05-2007
On Sep 30, 5:52 am, "Wolfgang Nádasi-Donner" <ed.oda...@wonado.de>
wrote:
> Filipe wrote:
> > puts showRE("Hello", /(\w)\1/) #=> no match
> > puts showRE("Hello", /(\w{1,1})\1/) #=> He<<ll>>o

>
> Both return "He<<ll>>o" on my machine too (Windows2000 -
> OneClickInstaller 186-25).
>
> Wolfgang Nádasi-Donner
> --
> Posted viahttp://www.ruby-forum.com/.


looks like the ruby interpreter I was running was jruby, under
netbeans. Unfourtunately, I didn't realize it at first.

 
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
regular expresion, search replace ask8y@yahoo.com Perl Misc 1 02-19-2013 04:06 AM
Newbie: Regular expresion Jose Luis Perl Misc 4 09-18-2009 04:12 PM
Regular Expresion Needed Matt White Ruby 2 06-19-2007 07:36 PM
Deriving boolean expresion from truth table dayzman@hotmail.com C++ 0 05-04-2005 04:02 AM
Why C++ has ambiguity between expresion statments and declaration statments? Andy C++ 5 01-23-2005 04:49 PM



Advertisments