我无法确定我的正则表达式失败的原因。这是我的代码:
my $email = "rise@dawn.com";
my ($found) = $email =~ /(rise@dawn\.com)/;
print "Found: $found";
这导致输出:
C:\scripts\perl\sandbox>regex.pl
Found: rise.com
如果我转义 @ 符号,则根本没有输出:
my $email = "rise@dawn.com";
my ($found) = $email =~ /(rise\@dawn\.com)/;
print "Found: $found";
C:\scripts\perl\sandbox>regex.pl
Found:
有人可以请教我我的错误。
最佳答案
永远 use strict; use warnings;
在脚本的顶部!
它会警告你有一个未声明的全局变量 @dawn
.数组也可以插入双引号字符串,所以你需要
my $email = "rise\@dawn.com";
my ($found) = $email =~ /(rise\@dawn\.com)/;
print "Found: $found";
关于regex - Perl 正则表达式 : Does the at sign @ need to be escaped?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17516845/