Forum: Ruby regexp problem - differences in Perl and Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E9488ef674513283e2822ba162559131?d=identicon&s=25 Sam Dela Cruz (Guest)
on 2006-01-10 02:47
(Received via mailing list)
I got different results in Perl and Ruby of this regular expression.
Can
somebody maybe give me a "Ruby Way" solution of this?  The output from
Perl is what I want.  But I'm currently programming this is Ruby.

In Perl:
#snippet start ==========
sub transform_data
{
    my $data = shift;
    if ($data=~/^[\d\.]+$/) #numbers
    {
        print "Got here!\n";
    }
    else
    {
        print "'$data'","\n";
    }
}

my $data = "patched 3 systems:
134.27.56.237
134.27.59.6
134.27.55.43";

transform_data($data);
#snippet end =========

Output is:
'patched 3 systems:
134.27.56.237
134.27.59.6
134.27.55.43'

---------------------------------------------------------------------------------------------------------------

In Ruby:
#snippet start ============
def transform_data(data)
    if (data=~/^[\d\.]+$/) #numbers
        puts "Got here!"
    else
        puts("'" + data + "'")
    end
end

data = "patched 3 systems:
134.27.56.237
134.27.59.6
134.27.55.43"

transform_data(data)
#snippet end ============

Output is:
Got here!

Now why would it match in Ruby?  Am I missing something here?

Regards,
Sam
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-01-10 02:56
(Received via mailing list)
Hi --

On Tue, 10 Jan 2006, Sam Dela Cruz wrote:

> end
> Got here!
>
> Now why would it match in Ruby?  Am I missing something here?

^ and $ match start and end of line, not string.  For start and end of
string, you want \A and \z (or \Z to ignore final newline).


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
E9488ef674513283e2822ba162559131?d=identicon&s=25 Sam Dela Cruz (Guest)
on 2006-01-10 03:05
(Received via mailing list)
Thanks David.

Yes, after I replaced ^ with \A and $ with \a, it works now.
Another difference learned.  I'm discovering new stuffs in Ruby every
day.

Regards,
Sam









dblack@wobblini.net
01/09/2006 05:54 PM
Please respond to
ruby-talk@ruby-lang.org


To
ruby-talk@ruby-lang.org (ruby-talk ML)
cc

Subject
Re: regexp problem - differences in Perl and Ruby
Classification







Hi --

On Tue, 10 Jan 2006, Sam Dela Cruz wrote:

> end
> Got here!
>
> Now why would it match in Ruby?  Am I missing something here?

^ and $ match start and end of line, not string.  For start and end of
string, you want \A and \z (or \Z to ignore final newline).


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
E9488ef674513283e2822ba162559131?d=identicon&s=25 Sam Dela Cruz (Guest)
on 2006-01-10 03:08
(Received via mailing list)
Oops sorry, typo, I mean when I replaced ^ with \A and $ with \z

Regards,
Sam









Sam Dela Cruz <sam.dela.cruz+FromInternet@philips.com>
01/09/2006 06:05 PM
Please respond to
ruby-talk@ruby-lang.org


To
ruby-talk@ruby-lang.org (ruby-talk ML)
cc

Subject
Re: regexp problem - differences in Perl and Ruby
Classification







Thanks David.

Yes, after I replaced ^ with \A and $ with \a, it works now.
Another difference learned.  I'm discovering new stuffs in Ruby every
day.

Regards,
Sam









dblack@wobblini.net
01/09/2006 05:54 PM
Please respond to
ruby-talk@ruby-lang.org


To
ruby-talk@ruby-lang.org (ruby-talk ML)
cc

Subject
Re: regexp problem - differences in Perl and Ruby
Classification







Hi --

On Tue, 10 Jan 2006, Sam Dela Cruz wrote:

> end
> Got here!
>
> Now why would it match in Ruby?  Am I missing something here?

^ and $ match start and end of line, not string.  For start and end of
string, you want \A and \z (or \Z to ignore final newline).


David

--
David A. Black
dblack@wobblini.net

"Ruby for Rails", from Manning Publications, coming April 2006!
http://www.manning.com/books/black
This topic is locked and can not be replied to.