Regular Expresion Error?

F

Filipe

Hello all,

According to the Regular Expressions section of the following ruby
guide: http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_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
 
S

Sebastian Hungerecker

Filipe said:
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]
 
W

Wolfgang Nádasi-Donner

Filipe said:
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
 
F

Filipe

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

Wolfgang Nádasi-Donner

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

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top