[nuby] it seems that (?!...) doesn't work for me

Discussion in 'Ruby' started by Lionel Thiry, Oct 13, 2004.

  1. Lionel Thiry

    Lionel Thiry Guest

    I'd like to make some kind of substitution like in a shell: "${var}".
    For that purpose, I use this pattern: /\$\{(.*?)\}/.

    But I also want to be able to escape those special caracters $, { and }.
    Examples:
    "${var\\}weird}" # which is a variable with "var{weird" as name
    "\\${var}" # which is the simple string "${var}" with no substitution

    Then I wrote that pattern:
    /(?!\\)\$\{(.*?)(?!\\)\}/

    But, well, it doesn't work at all.

    After some tests:
    puts "ok" if "\\{".match(/\\\{/) # display "ok"
    puts "not ok" if "\\{".match(/(?!\\)\{/) # display "not ok"

    It seems that (?!\\) doesn't fullfill its role of "match if re in (?!re)
    doesn't match". What's wrong with my code? Please help!

    My ruby version is the "One-Click Installer - Windows" v1.8.1-13.

    Lio
     
    Lionel Thiry, Oct 13, 2004
    #1
    1. Advertising

  2. Lionel Thiry

    ts Guest

    >>>>> "L" == Lionel Thiry <> writes:

    L> It seems that (?!\\) doesn't fullfill its role of "match if re in (?!re)
    L> doesn't match". What's wrong with my code?

    (?!re) is a zero-width negative look-*ahead* assertion. Apparently you
    want a zero-width negative look-*behind* assertion which is (?<!re) and
    implemented only in Oniguruma


    svg% cat b.rb
    #!./ruby
    ["${var\\}weird}", "\\${var}", "${var}"].each do |m|
    p "#{m} -- #$1" if m.match(/(?<!\\)\$\{(.*?)(?<!\\)\}/)
    end
    svg%

    svg% ruby b.rb
    "${var\\}weird} -- var\\}weird"
    "${var} -- var"
    svg%



    Guy Decoux
     
    ts, Oct 13, 2004
    #2
    1. Advertising

  3. Lionel Thiry

    Lionel Thiry Guest

    ts a écrit :

    >>>>>>"L" == Lionel Thiry <> writes:

    >
    >
    > L> It seems that (?!\\) doesn't fullfill its role of "match if re in (?!re)
    > L> doesn't match". What's wrong with my code?
    >
    > (?!re) is a zero-width negative look-*ahead* assertion. Apparently you
    > want a zero-width negative look-*behind* assertion which is (?<!re) and
    > implemented only in Oniguruma

    That's right, that's probably what I want... but how do I use oniguruma?



    Lionel Thiry
     
    Lionel Thiry, Oct 13, 2004
    #3
  4. Lionel Thiry

    ts Guest

    >>>>> "L" == Lionel Thiry <> writes:

    L> That's right, that's probably what I want... but how do I use oniguruma?

    Oniguruma is in 1.9, but be carefull and don't forget that the regexp
    engine is stupid

    svg% ruby -e 'puts "not" unless "\\\\${var}".match(/(?<!\\)\$\{(.*?)(?<!\\)\}/)'
    not
    svg%



    Guy Decoux
     
    ts, Oct 13, 2004
    #4
  5. Lionel Thiry wrote:

    > I'd like to make some kind of substitution like in a shell: "${var}".
    > For that purpose, I use this pattern: /\$\{(.*?)\}/.
    >
    > But I also want to be able to escape those special caracters $, { and
    > }. Examples:
    > "${var\\}weird}" # which is a variable with "var{weird" as name
    > "\\${var}" # which is the simple string "${var}" with no substitution


    Use: /(^|[^\\]|(\\\\)+)\$\{(([^\}\\]|(\\.))*)\}/

    I testes this with:
    puts
    (STDIN.readlines.join("\n").gsub(/(^|[^\\]|(\\\\)+)\$\{(([^\}\\]|(\\.))*)\}/m)
    {|m| "#{$1}--#{$3.gsub(/\\(.)/, "\\1")}--"})
    And it seems to work (replaces ${var} with --var--, but only if not
    escaped).

    The first part (/(^|[^\\]|(\\\\)+)/) basically means "either
    start-of-string, non-backslash, or an even amount of backslash".
    The part (/\{(([^\}\\]|(\\.))*)\}/) means "start with {, and than allow
    any number of groups that consist of either a sequence of non-} and
    non-\, or
    of an escaped character (/\\./).

    greetings, Florian Pflug
     
    Florian G. Pflug, Oct 13, 2004
    #5
  6. Lionel Thiry

    Lionel Thiry Guest

    Florian G. Pflug a écrit :

    > Lionel Thiry wrote:
    >
    >> I'd like to make some kind of substitution like in a shell: "${var}".
    >> For that purpose, I use this pattern: /\$\{(.*?)\}/.
    >>
    >> But I also want to be able to escape those special caracters $, { and
    >> }. Examples:
    >> "${var\\}weird}" # which is a variable with "var{weird" as name
    >> "\\${var}" # which is the simple string "${var}" with no substitution

    >
    >
    > Use: /(^|[^\\]|(\\\\)+)\$\{(([^\}\\]|(\\.))*)\}/
    >
    > I testes this with:
    > puts
    > (STDIN.readlines.join("\n").gsub(/(^|[^\\]|(\\\\)+)\$\{(([^\}\\]|(\\.))*)\}/m)
    > {|m| "#{$1}--#{$3.gsub(/\\(.)/, "\\1")}--"})
    > And it seems to work (replaces ${var} with --var--, but only if not
    > escaped).
    >
    > The first part (/(^|[^\\]|(\\\\)+)/) basically means "either
    > start-of-string, non-backslash, or an even amount of backslash".
    > The part (/\{(([^\}\\]|(\\.))*)\}/) means "start with {, and than allow
    > any number of groups that consist of either a sequence of non-} and
    > non-\, or
    > of an escaped character (/\\./).
    >
    > greetings, Florian Pflug


    Thanks a lot for the help, but the pattern you gave is not perfect:

    re = /(^|[^\\]|(\\\\)+)\$\{(([^\}\\]|(\\.))*)\}/
    'pre${var}'.match # => prematch is "pr"

    Lionel Thiry
     
    Lionel Thiry, Oct 14, 2004
    #6
    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. Frederic Gignac

    Expiration date seems not to work

    Frederic Gignac, Jul 4, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    428
    Frederic Gignac
    Jul 8, 2003
  2. Boris \BXS\ Schulz

    nuby question: f.rename(x,y) does not work

    Boris \BXS\ Schulz, Jan 3, 2004, in forum: Ruby
    Replies:
    4
    Views:
    96
    Boris \BXS\ Schulz
    Jan 3, 2004
  3. Alexey Verkhovsky
    Replies:
    8
    Views:
    167
    Hal Fulton
    Jul 18, 2004
  4. Steve Hill

    [nuby] Can't get rails to work

    Steve Hill, Oct 1, 2004, in forum: Ruby
    Replies:
    3
    Views:
    118
    David Heinemeier Hansson
    Oct 4, 2004
  5. ry
    Replies:
    1
    Views:
    83
    Nobuyoshi Nakada
    Mar 17, 2008
Loading...

Share This Page