oniguruma : can't get it to search for \ (backslash)

Discussion in 'Ruby' started by Dean Holdren, Jun 26, 2007.

  1. Dean Holdren

    Dean Holdren Guest

    I can't seem to get Oniguruma to look for a single backslash, am I
    doing anything wrong here?:

    I should be able to escape the single with another:
    irb(main):043:0> reg = Oniguruma::ORegexp.new('\\')
    ArgumentError: Oniguruma Error: end pattern at escape
    from /usr/local/lib/ruby/site_ruby/1.8/oniguruma.rb:177:in
    `old_initialize'
    from /usr/local/lib/ruby/site_ruby/1.8/oniguruma.rb:177:in `initialize'
    from (irb):43:in `new'
    from (irb):43
    from /usr/local/lib/ruby/site_ruby/1.8/oniguruma.rb:359

    For arguments sake, with just one backslash:
    irb(main):044:0> reg = Oniguruma::ORegexp.new('\')
    irb(main):045:1'
    (with just one back-slash, irb sees unfinished syntax)

    Same if I use double-quotes:
    irb(main):046:0> reg = Oniguruma::ORegexp.new("\\")
    ArgumentError: Oniguruma Error: end pattern at escape
    from /usr/local/lib/ruby/site_ruby/1.8/oniguruma.rb:177:in
    `old_initialize'
    from /usr/local/lib/ruby/site_ruby/1.8/oniguruma.rb:177:in `initialize'
    from (irb):46:in `new'
    from (irb):46
    from /usr/local/lib/ruby/site_ruby/1.8/oniguruma.rb:359

    Double quotes with just one backslash:
    irb(main):047:0> reg = Oniguruma::ORegexp.new("\")
    irb(main):048:1"
    Dean Holdren, Jun 26, 2007
    #1
    1. Advertising

  2. Dean Holdren wrote:
    > I can't seem to get Oniguruma to look for a single backslash, am I
    > doing anything wrong here?:
    >
    > I should be able to escape the single with another:
    > irb(main):043:0> reg = Oniguruma::ORegexp.new('\\')
    > ArgumentError: Oniguruma Error: end pattern at escape


    In a regex literal a backslash is a metacharacter, so you need two
    slashes to match a slash: /\\/ But to insert two consecutive slashes in
    a string to be compiled into a regex, you need four slashes: "\\\\"

    Observe:

    irb> reg = Oniguruma::ORegexp.new('\\\\')
    => /\\/

    best,
    Dan

    --
    Posted via http://www.ruby-forum.com/.
    Daniel Lucraft, Jun 26, 2007
    #2
    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. Vincent Texier
    Replies:
    3
    Views:
    2,251
    Vincent Texier
    Jul 1, 2004
  2. Simon Strandgaard

    regexp unlimited: Ruby's vs Oniguruma

    Simon Strandgaard, Nov 13, 2003, in forum: Ruby
    Replies:
    2
    Views:
    78
    Mark Wilson
    Nov 17, 2003
  3. Phil Tomson

    look-behind in oniguruma

    Phil Tomson, Sep 12, 2004, in forum: Ruby
    Replies:
    5
    Views:
    129
    Simon Strandgaard
    Sep 12, 2004
  4. Wolfgang Nádasi-Donner
    Replies:
    8
    Views:
    157
    Wolfgang Nádasi-Donner
    Jul 31, 2005
  5. Phil Tomson
    Replies:
    1
    Views:
    100
    Phil Tomson
    Dec 31, 2007
Loading...

Share This Page