regex bug?

Z

Zach Dennis

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
 
H

Hal Fulton

Zach said:
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?

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
 
J

John W. Long

-----"Zach Dennis" wrote:-----
command.gsub!( /\[#{key}\]/ , "#{attrib}" )

replace this line with:
command.gsub!( /\[#{key}\]/ , "#{Regexp::quote(attrib)}" )
Any idea why it keeps stealing my \4?

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 ''.
 
M

Mike Stok

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\\"

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(/\\/, '\\\\\\')

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 parse
the replacement string for you (but does have some overhead):

[mike@ratdog mike]$ irb
key = "file" => "file"
attrib = "\\Applications\\4Dapp\\Chess\\" => "\\Applications\\4Dapp\\Chess\\"
command = "xcopy [file] /y /s /e i:\\source\\" => "xcopy [file] /y /s /e i:\\source\\"
command.gsub!(/\[#{key}\]/) { attrib } => "xcopy \\Applications\\4Dapp\\Chess\\ /y /s /e i:\\source\\"
puts command
xcopy \Applications\4Dapp\Chess\ /y /s /e i:\source\
=> nil

Hope this helps,

Mike
 

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
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top