# Regexp problem - differences in Perl and Ruby

**URL:** <https://www.ruby-forum.com/t/regexp-problem-differences-in-perl-and-ruby/52013>\
**Category:** Ruby\
**Created:** [January 10, 2006, 2:47am UTC](https://www.ruby-forum.com/t/regexp-problem-differences-in-perl-and-ruby/52013 "2006-01-10T02:47:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 10, 2006, 2:47am UTC](https://www.ruby-forum.com/t/regexp-problem-differences-in-perl-and-ruby/52013/1 "2006-01-10T02:47:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 10, 2006, 2:56am UTC](https://www.ruby-forum.com/t/regexp-problem-differences-in-perl-and-ruby/52013/2 "2006-01-10T02:56:56Z")

</div>

Hi –

On Tue, 10 Jan 2006, Sam Dela C. 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  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 10, 2006, 3:05am UTC](https://www.ruby-forum.com/t/regexp-problem-differences-in-perl-and-ruby/52013/3 "2006-01-10T03:05:58Z")

</div>

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

removed\_email\_address@domain.invalid  
01/09/2006 05:54 PM  
Please respond to  
removed\_email\_address@domain.invalid

To  
removed\_email\_address@domain.invalid (ruby-talk ML)  
cc

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

Hi –

On Tue, 10 Jan 2006, Sam Dela C. 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  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 10, 2006, 3:08am UTC](https://www.ruby-forum.com/t/regexp-problem-differences-in-perl-and-ruby/52013/4 "2006-01-10T03:08:59Z")

</div>

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

Regards,  
Sam

Sam Dela C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
01/09/2006 06:05 PM  
Please respond to  
removed\_email\_address@domain.invalid

To  
removed\_email\_address@domain.invalid (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

removed\_email\_address@domain.invalid  
01/09/2006 05:54 PM  
Please respond to  
removed\_email\_address@domain.invalid

To  
removed\_email\_address@domain.invalid (ruby-talk ML)  
cc

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

Hi –

On Tue, 10 Jan 2006, Sam Dela C. 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  
removed\_email\_address@domain.invalid

“Ruby for Rails”, from Manning Publications, coming April 2006!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...
