From: Brian Candler Date: 2003-04-27T21:04:33+09:00 Subject: Re: Regexp and $ On Sun, Apr 27, 2003 at 08:41:04PM +0900, I wrote: > This means the description "$ matches end of string or before \n" is wrong > for Ruby. In fact, I'm not sure *what* exactly $ matches. Can anybody give > me a description of what $ matches in Ruby? The closest description I can come up with is: In Ruby, '$' matches: - before a newline - at the end of the string, UNLESS the last character of the string is a newline, in which case it doesn't match there. irb(main):001:0> a = "abc\n\n" => "abc\n\n" irb(main):002:0> a.gsub(/$/,'z') => "abcz\nz\n" irb(main):003:0> a = "abc" => "abc" irb(main):004:0> a.gsub(/$/,'z') => "abcz" I wonder why this was chosen rather that just "'$' matches before a newline *and* at the end of the string"? I guess if you do gsub(/$/,'foo') to add text to the end of a \n-terminated line then it kind of makes sense. But it does stop you matching /\n$/ if you want to explicitly eat up newlines - and breaks compatibility with other languages. '^' doesn't have this issue: irb(main):005:0> b = "\nabc" => "\nabc" irb(main):006:0> b.gsub(/^/,'z') => "z\nzabc" So '^' always matches at the start of the string *and* after a newline. Regards, Brian.