gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:26:10 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com