Forum: Ruby Ruby RE expression anomaly.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
RichardOnRails (Guest)
on 2009-02-20 01:00
(Received via mailing list)
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, '<p><a HREF="\1\2"> \2 </a></p>')
  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 </a><br>\n"
"3:  <a HREF=\"file:\\\\F:\\Program Files\\Microsoft Office\\Visio11\
\VISIO.EXE\"> VISIO.EXE </a><br>\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
7stud -. (Guest)
on 2009-02-20 02:28
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
botp (Guest)
on 2009-02-20 05:02
(Received via mailing list)
From: 7stud -- [mailto:removed_email_address@domain.invalid]
# 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 :)

kind regards -botp
(replying in gmail; apologies in advance if this gets duplicated, my
outlook program is acting weird :)
Robert K. (Guest)
on 2009-02-20 17:20
(Received via mailing list)
2009/2/19 RichardOnRails <removed_email_address@domain.invalid>:
> 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
RichardOnRails (Guest)
on 2009-02-21 21:52
(Received via mailing list)
On Feb 19, 10:00 pm, botp <removed_email_address@domain.invalid> wrote:
>
> kind regards -botp
> (replying in gmail; apologies in advance if this gets duplicated, my
> outlook program is acting weird :)

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 :)

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
Simon K. (Guest)
on 2009-02-24 09:05
(Received via mailing list)
* 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 </a><br>\n"
> "3:  <a HREF=\"file:\\\\F:\\Program Files\\Microsoft Office\\Visio11\
> \VISIO.EXE\"> VISIO.EXE </a><br>\n"
> [snip]

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

mfg,                          simon .... l
This topic is locked and can not be replied to.