[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Newlines included in bracket negation

7stud --

10/26/2007 9:38:00 PM

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-....

1 Answer

Jesús Gabriel y Galán

10/26/2007 9:46:00 PM

0

On 10/26/07, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> 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.

Can you check my example above? I'm using a greedy match of .* which I
thought would match up to a \n in a non-multiline regexp, and would
include everything in a multiline one. I must be confused at some
point :-(

Jesus.