Hi --

On Tue, 19 Sep 2006, Paul Lutus wrote:

> Pit Capitain wrote:
>
>> Paul Lutus schrieb:
>>> Bart Braem wrote:
>>>>
>>>> I will try it with:
>>>> string =~ /^\w+\.\w+\z/
>>>>
>>>> But is there a ruby operator to look for matches on the entire string?
>>>
>>> string =~ /^\w+\.\w+$/
>>>
>>> Must match the entire string. Because of multiline issues, where that is
>>> a factor, you can always split on newlines in advance of this test to be
>>> sure you are matching the entire string unambiguously.
>>
>> 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.

>> Just use Bart's regexp (with \z
>> instead of $). It matches the entire string.
>
> 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 | dblack / wobblini.net
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