regexp problem - differences in Perl and Ruby

Discussion in 'Ruby' started by Sam Dela Cruz, Jan 10, 2006.

  1. --=_alternative 0009AE3C882570F2_=
    Content-Type: text/plain; charset="US-ASCII"

    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
    --=_alternative 0009AE3C882570F2_=--
     
    Sam Dela Cruz, Jan 10, 2006
    #1
    1. Advertising

  2. Sam Dela Cruz

    Guest

    Hi --

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

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


    ^ 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


    "Ruby for Rails", from Manning Publications, coming April 2006!
    http://www.manning.com/books/black
     
    , Jan 10, 2006
    #2
    1. Advertising

  3. --=_alternative 000B79BE882570F2_=
    Content-Type: text/plain; charset="US-ASCII"

    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










    01/09/2006 05:54 PM
    Please respond to



    To
    (ruby-talk ML)
    cc

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







    Hi --

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

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


    ^ 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


    "Ruby for Rails", from Manning Publications, coming April 2006!
    http://www.manning.com/books/black



    --=_alternative 000B79BE882570F2_=--
     
    Sam Dela Cruz, Jan 10, 2006
    #3
  4. --=_alternative 000BC0FF882570F2_=
    Content-Type: text/plain; charset="US-ASCII"

    Oops sorry, typo, I mean when I replaced ^ with \A and $ with \z

    Regards,
    Sam









    Sam Dela Cruz <>
    01/09/2006 06:05 PM
    Please respond to



    To
    (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










    01/09/2006 05:54 PM
    Please respond to



    To
    (ruby-talk ML)
    cc

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







    Hi --

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

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


    ^ 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


    "Ruby for Rails", from Manning Publications, coming April 2006!
    http://www.manning.com/books/black




    --=_alternative 000BC0FF882570F2_=--
     
    Sam Dela Cruz, Jan 10, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Home_Job_opportunity
    Replies:
    0
    Views:
    513
    Home_Job_opportunity
    Jan 8, 2009
  2. Home_Job_opportunity
    Replies:
    0
    Views:
    597
    Home_Job_opportunity
    Jan 14, 2009
  3. Chris Meyers

    Ruby Regexp vs Perl and C#

    Chris Meyers, Oct 13, 2006, in forum: Ruby
    Replies:
    6
    Views:
    165
    Martin DeMello
    Oct 17, 2006
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    512
    Mikel Lindsaar
    Mar 31, 2008
  5. Joao Silva
    Replies:
    16
    Views:
    382
    7stud --
    Aug 21, 2009
Loading...

Share This Page