Ruby RE expression anomaly

Hi,

I’m trying to use Ruby REs to transform Windows addresses into HTML
anchors.

CODE:
DEBUG = 1
fo= File.open(“Microsoft EXEs xformed.txt”, “w”)
fi= File.new(“Microsoft EXEs.txt”)
fi.each { |line|
next unless line =~ /11/
p "1: " + line if DEBUG
line.sub!( /^(.*\)(\w+.\w+)$/i, ’

\2

')
p “2: " + line if DEBUG
line.sub!(/(HREF=”)/, ‘\1file:\\\’)
p "3: " + line if DEBUG
fo.write(line)
}
fi.close
fo.close

OUTPUT (in Sci/TE:)
“1: F:\Program Files\Microsoft Office\Visio11\VISIO.EXE\n”
"2: <a HREF="F:\Program Files\Microsoft Office\Visio11\VISIO.EXE
“> VISIO.EXE
\n”
“3: <a HREF=“file:\\F:\Program Files\Microsoft Office\Visio11
\VISIO.EXE”> VISIO.EXE
\n”
[snip]

Note that output displayed within Sci/TE has all backslashes and
quotes escaped with prefixed backslashes.

QUESTION 1:
Displayed line 3 appears to have only two escaped backslashes at the
start of the HREF address.
However my 2nd substitution supplies three escaped backslashes.
Furthermore, the 1st line of the output file confirms that only two
backslashes were inserted.
Can you see any reason for not getting all three backslashes specified
in the RE?

QUESTION 2:
This question may be inappropriate for this newsgroup, so please
ignore it unless you care to answer it.
Happily, the two slashes generated above were sufficient for Firefox
3 running over WinXP-Pro/SP3 to recognize that the HREF referred to a
local file. (I thought I need three slashes for that.)

However, Firefox (I guess) blocked the execution of it and offered
only to save it. I’m running under Admin privileges on my own home
machine. Is there anyway for me to the Windows program launched?

Thanks in Advance,
Richard

RichardOnRails wrote:

  1. My tests show that you need 10 backslashes in the substitution to get
    3 in the final result.

line = “HREF=”
line.sub!(/(HREF=)/, ‘\1file:\\\\\’)

p line
puts line

–output:–
“HREF=file:\\\”
HREF=file:\\

10 backslashes in sub
----------------------- = 3.3 backslashes per desired backslash. lol.
3 backslashes in final result

2009/2/19 RichardOnRails [email protected]:

I’m trying to use Ruby REs to transform Windows addresses into HTML
anchors.

CODE:
DEBUG = 1
fo= File.open(“Microsoft EXEs xformed.txt”, “w”)
fi= File.new(“Microsoft EXEs.txt”)
fi.each { |line|

}
fi.close
fo.close

I suggest rewriting this as

File.open “Microsoft EXEs.txt” do |fi|
File.open “Microsoft EXEs xformed.txt”, “w” do |fo|
fi.each …
end
end

First, the block form of File.open is much safer because then file
handles will always be closed which is not the case for your
version.

Second, do the less dangerous operation first, because if that fails
you do not accidentally overwrite file “Microsoft EXEs xformed.txt”.

Cheers

robert

From: 7stud – [mailto:[email protected]]

backslash. lol.

3 backslashes in final result

you can do,

line.sub!(/(HREF=)/){|x| x+‘file:\\\’}

well, if you work on strings and on OSes that use backslashes as
file/dir separators, it will be fun-ny :slight_smile:

kind regards -botp
(replying in gmail; apologies in advance if this gets duplicated, my
outlook program is acting weird :slight_smile:

  • RichardOnRails (2009-02-19) schrieb:

OUTPUT (in Sci/TE:)
“1: F:\Program Files\Microsoft Office\Visio11\VISIO.EXE\n”
"2: <a HREF="F:\Program Files\Microsoft Office\Visio11\VISIO.EXE
“> VISIO.EXE
\n”
“3: <a HREF=“file:\\F:\Program Files\Microsoft Office\Visio11
\VISIO.EXE”> VISIO.EXE
\n”
[snip]

Replace these backslashes with slashes. Even Windows accepts this and
it’s legal URI syntax.

mfg, simon … l

On Feb 19, 10:00 pm, botp [email protected] wrote:

kind regards -botp
(replying in gmail; apologies in advance if this gets duplicated, my
outlook program is acting weird :slight_smile:

Hi botp,

Thanks for your response. It somehow gets around the problem with my
approach, which seems to perform string-substitution twice, as I
mentioned to the previous respondent.

Just for the record, I had to sub the string HREF=" to get the result
I was after.

well, if you work on strings and on OSes that use backslashes as
file/dir separators, it will be fun-ny :slight_smile:

I am working on WinXP, which as you no doubt know uses backslashes for
separators. Thanks to the previous respondent’s use of puts in place
of p, the debugging output looks much neater. And sticking file:\
in front of a name of a file-system object gets Windows to handle the
local file through the browser. The only problem for my purposes is
(apparently) that the browser won’t invoke the local file; it only
permits saving a copy of it somewhere else in the file-system.

Again, thanks for your very neat expression.

Best wishes,
Richard