From: 7stud -- Date: 2007-10-27T06:38:08+09:00 Subject: Re: Newlines included in bracket negation Chris Morris wrote: > (... that subject probably makes no sense ...) > > Anyway, I have some unexpected (to me) behavior in the following regexp. > This example is contrived, but based on a real need. Can anyone explain > why > the result is multi-line, even though the re is not? > > require 'test/unit' > > class TestRE < Test::Unit::TestCase > def test_newlines > src = "happy\n\nbirthday" > assert_equal("hday", src.scan(/h[^x]*?day/).to_s) > end > end > > produces > > Finished in 0.031 seconds. > > 1) Failure: > test_newlines_consumed_in_not_section(TestRE) ... > <"hday"> expected but was > <"happy\n\nbirthday">. > Can anyone explain why > the result is multi-line, even though the re is not? It's not a question of the re being multi-line or not, it's a question of the re being greedy v. non-greedy. But because there is only one match for your regex, the issue of greedy v. non-greedy is irrelevant. If you think about it, there is really no concept of 'lines' with regards to text. There really is only one line--one, long, continuous line of characters. Some of those characters might be '\n' characters, and we may choose to interpret a '\n' as a new line, but that doesn't change the fact that there is still just one continuous string of characters. A regex has nothing inherently programmed into it that will cause it to stop looking for matches when a '\n' is encountered in the sequence of characters. The regex character '.' will stop searching at a newline, but that is not true of regex's generally. In any case, you do not use the '.' character in your regex, so that behavior is irrelevant. -- Posted via http://www.ruby-forum.com/.