From: Brian Candler Date: 2011-03-28T17:59:12+09:00 Subject: Re: How to "find" new lines Chris Kottom wrote in post #989369: > 7stud is right. I missed the important word "only" in your original > email. > My original method will produce false positives if you are indeed > interested in finding strings which are only "\n". > > The "==" method works fine. Regexp is also possible (though probably > slower) if done as: > > if str =~ /^\n$/ Wrong - that doesn't match only strings containing just a newline. >> str = "\n" => "\n" >> str =~ /^\n$/ => 0 >> str = "\n\n" => "\n\n" >> str =~ /^\n$/ => 0 >> str = "\n\nabc" => "\n\nabc" >> str =~ /^\n$/ => 0 With a regexp, you need str =~ /\A\n\z/ >> str = "\n" => "\n" >> str =~ /\A\n\z/ => 0 >> str = "\n\n" => "\n\n" >> str =~ /\A\n\z/ => nil >> str = "\n\nabc" => "\n\nabc" >> str =~ /\A\n\z/ => nil (beware: this is an area where Ruby's regexps are *not* the same as Perl's regexps) -- Posted via http://www.ruby-forum.com/.