Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl Regex: Does the at sign @ need to be escaped?

Tags:

regex

perl

I'm unable to determine why my regex is failing. Here is my code:

my $email = "[email protected]";
my ($found) = $email =~ /(rise@dawn\.com)/;
print "Found: $found";

This results in the output:

C:\scripts\perl\sandbox>regex.pl
Found: rise.com

If I escape the @ sign, then I get no output at all:

my $email = "[email protected]";
my ($found) = $email =~ /(rise\@dawn\.com)/;
print "Found: $found";

C:\scripts\perl\sandbox>regex.pl
Found:

Could someone please enlighten me as to my errors.

like image 803
Dirty Penguin Avatar asked Jul 07 '13 22:07

Dirty Penguin


2 Answers

in your declaration of $email you are interpolating @dawn This is due to quoting.

To avoid any hassle, just use single quotes like this:

my $email = '[email protected]';
my ($found) = $email =~ /(rise\@dawn\.com)/;
print "Found: $found";
like image 145
Niko S P Avatar answered Sep 22 '22 18:09

Niko S P


Always use strict; use warnings; at the top of your scripts!

It would warn you that there is an undeclared global variable @dawn. Arrays can be interpolated into double quoted strings as well, so you need

my $email = "rise\@dawn.com";
my ($found) = $email =~ /(rise\@dawn\.com)/;
print "Found: $found";
like image 35
amon Avatar answered Sep 19 '22 18:09

amon