Professional Web Applications Themes

regex bug? - Ruby

key = "file" attrib = "\Applications\4Dapp\Chess\" command = "xcopy [file] /y /s /e i:\source\" command.gsub!( /\[#{key}\]/ , "#{attrib}" ) puts command => "xcopy \ApplicationsDapp\Chess\ /y /s /e i:\source\" It should be: => "xcopy \Applications\4Dapp\Chess\ /y /s /e i:\source\" Any idea why it keeps stealing my ? Zach...

  1. #1

    Default regex bug?

    key = "file"
    attrib = "\\Applications\\4Dapp\\Chess\\"
    command = "xcopy [file] /y /s /e i:\\source\\"
    command.gsub!( /\[#{key}\]/ , "#{attrib}" )
    puts command
    => "xcopy \\ApplicationsDapp\\Chess\\ /y /s /e i:\\source\\"



    It should be:

    => "xcopy \\Applications\\4Dapp\\Chess\\ /y /s /e i:\\source\\"


    Any idea why it keeps stealing my \4?

    Zach




    Zach Guest

  2. #2

    Default Re: regex bug?

    Zach Dennis wrote: 

    It recognizes \4 as meaning the fourth submatch of the
    last regex, or something to that effect.

    Double escaping or something should fix it.

    Hal



    Hal Guest

  3. #3

    Default Re: regex bug?

    -----"Zach Dennis" wrote:----- 

    replace this line with:
    command.gsub!( /\[#{key}\]/ , "#{Regexp::quote(attrib)}" )
     

    Because in gsub terms \4 represents the forth grouping:

    "123412341234".gsub(/(.)(.)(.)(.)/, '\4')
    => "444"

    You don't have any groupings in your expression so '\4' is equivalent to ''.

    --
    John




    John Guest

  4. #4

    Default Re: regex bug?

    On Sat, Jan 31, 2004 at 01:07:42PM +0900, Zach Dennis wrote: 

    As others have said, '\4' in a gsub string is special. To get around
    this you need yet another backslash in front of it, which of course
    means two more backslashes between the double quotes:

    attrib = "\\Applications\\\\4Dapp\\Chess\\"

    You can safely double all backslashes in gsub strings, so you might just
    want to make it special-processing-proof automatically with some
    preparatory munging of your own:

    attrib = "\\Applications\\4Dapp\\Chess\\".gsub(/\\/, '\\\\\\')

    Just as a side note, if you're going to be making literal strings with
    backslashes, single quotes cut down on the number of doublings required:

    attrib = '\Applications\4Dapp\Chess\\'.gsub(/\\/, '\\\\\\')

    -Mark
    Mark Guest

  5. #5

    Default Re: regex bug?

    In article <thereeds.org>,
    Mark J. Reed <com> wrote: 
    >
    >As others have said, '\4' in a gsub string is special. To get around
    >this you need yet another backslash in front of it, which of course
    >means two more backslashes between the double quotes:
    >
    > attrib = "\\Applications\\\\4Dapp\\Chess\\"
    >
    >You can safely double all backslashes in gsub strings, so you might just
    >want to make it special-processing-proof automatically with some
    >preparatory munging of your own:
    >
    > attrib = "\\Applications\\4Dapp\\Chess\\".gsub(/\\/, '\\\\\\')
    >
    >Just as a side note, if you're going to be making literal strings with
    >backslashes, single quotes cut down on the number of doublings required:
    >
    > attrib = '\Applications\4Dapp\Chess\\'.gsub(/\\/, '\\\\\\')[/ref]

    also if your replacement includes \ then you might consider using the
    block form of gsub or gsub! to do the replacement as it doesn't p
    the replacement string for you (but does have some overhead):

    [mikeratdog mike]$ irb [/ref]
    => "file" [/ref]
    => "\\Applications\\4Dapp\\Chess\\" [/ref]
    => "xcopy [file] /y /s /e i:\\source\\" [/ref]
    => "xcopy \\Applications\\4Dapp\\Chess\\ /y /s /e i:\\source\\" [/ref]
    xcopy \Applications\4Dapp\Chess\ /y /s /e i:\source\
    => nil

    Hope this helps,

    Mike

    --
    co.uk | The "`Stok' disclaimers" apply.
    http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
    com | Fingerprint 0570 71CD 6790 7C28 3D60
    http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
    Mike Guest

Similar Threads

  1. Regex help
    By powerranger in forum ASP.NET General
    Replies: 11
    Last Post: March 15th, 07:16 PM
  2. regex help
    By Andrew in forum PERL Beginners
    Replies: 7
    Last Post: September 24th, 06:02 AM
  3. REGEX help pls
    By apropo in forum PHP Development
    Replies: 1
    Last Post: September 12th, 07:11 PM
  4. regex, is this possible?
    By Jerry Preston in forum PERL Beginners
    Replies: 2
    Last Post: October 8th, 09:23 AM
  5. another how to regex ?
    By Jerry Preston in forum PERL Beginners
    Replies: 4
    Last Post: October 7th, 03:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139