Hi --
Pit said:
This would be unnecessarily complex.
Not really.
-----------------------------------
#!/usr/bin/ruby
s = "this is\na test\nstring."
a = []
a << s.sub(/(^.*$)/,"\\1")
That replaces "this is" with "this is".
a << s.sub(/(^.*\z)/,"\\1")
That replaces "string." with "string.".
a << s.sub(/(^.*$)/m,"\\1")
That replaces "this is\na test\nstring." with "this is\na test\n
string."
a << s.sub(/(^.*\z)/m,"\\1")
That does the same thing as the previous one.
In all of your examples, you're just replacing what was matched with
what was matched. That doesn't tell you anything *about* what was
matched.
My point is that, if there are embedded linefeeds and they are an issue,
they must be dealt with. Also, I don't immediately see a difference in
behavior between \z and $, contrary to the documentation's specification
that one matches the end of a line and the other matches the entire string.
You don't see it because you're not looking for it

Look at the
difference in what gets matched:
irb(main):011:0> s =~ /(^.*$)/
=> 0
irb(main):012:0> $1
=> "this is"
irb(main):013:0> s =~ /(^.*\z)/
=> 15
irb(main):014:0> $1
=> "string."
irb(main):015:0> s =~ /(^.*\z)/m
=> 0
irb(main):016:0> $1
=> "this is\na test\nstring."
David
--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1]
http://www.manning.com/black | [3]
http://www.rubypowerandlight.com
[2]
http://dablog.rubypal.com | [4]
http://www.rubycentral.org