regex - Perl 正则表达式 : Does the at sign @ need to be escaped?

标签 regex perl

我无法确定我的正则表达式失败的原因。这是我的代码:

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/

相关文章:

perl - 一些 Perl 的微妙之处

regex - Perl正则表达式-gc修饰符是什么意思?

javascript - XSD 正则表达式 : empty string OR something else

arrays - 如何搜索字符串中正则表达式模式的重叠匹配项

javascript - 为什么这个 Javascript RegExp 测试不起作用?

perl,使用 File::Find 是否可以修改该函数之外的数据结构?

linux - 如何在XML文件中的单词后面添加变量内容

regex - 在逗号分隔的数字中实现正则表达式

c++ - 使用 wregex 会产生垃圾输出?

perl - 如何在 Perl 中修改二进制标量的特定部分?