Regular Expresion Error?

Discussion in 'Ruby' started by Filipe, Sep 29, 2007.

  1. Filipe

    Filipe Guest

    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
     
    Filipe, Sep 29, 2007
    #1
    1. Advertising

  2. Filipe

    Filipe Guest

    Forgot to mention: Im running ruby 1.8.6 at Mac OS X 10.4.
     
    Filipe, Sep 29, 2007
    #2
    1. Advertising

  3. 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
     
    Sebastian Hungerecker, Sep 30, 2007
    #3
  4. 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/.
     
    Wolfgang Nádasi-Donner, Sep 30, 2007
    #4
  5. Filipe

    Filipe Guest

    On Sep 30, 5:52 am, "Wolfgang Nádasi-Donner" <>
    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.
     
    Filipe, Oct 5, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    5
    Views:
    488
    Ron Natalie
    Jan 23, 2005
  2. John Banks

    Expresion Expected Error - VS 2005 - Migrated from VS 2003

    John Banks, Jan 25, 2007, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    168
  3. Matt White

    Regular Expresion Needed

    Matt White, Jun 19, 2007, in forum: Ruby
    Replies:
    2
    Views:
    105
    Bertram Scharpf
    Jun 19, 2007
  4. Jose Luis

    Newbie: Regular expresion

    Jose Luis, Sep 18, 2009, in forum: Perl Misc
    Replies:
    4
    Views:
    111
    Steve C
    Sep 18, 2009
  5. Replies:
    1
    Views:
    173
    Jürgen Exner
    Feb 19, 2013
Loading...

Share This Page