我的脚本中有以下代码。
if($description =~ /\'/) {
print "I am here\n";
$description =~ s/\'/'/g;
}
print "description = $description\n";
当我运行此脚本时,由于比较失败,我没有得到"I am here\n"
输出。
但是,$description
字符串确实包含撇号。
$description = "The baseball player’s spiritual redemption and recovery from drug addiction.";
上下文:我正在解析从调用nytimes bestsellers api(以json格式返回)获得的字符串,并且此字符串存储在$description
字符串中。
答案 0 :(得分:6)
您的示例字符串不包含撇号。它包含U+2019 RIGHT SINGLE QUOTATION MARK
。
它应匹配/\x{2019}/
答案 1 :(得分:0)
您不需要在正则表达式中转义引号。稍微改变你的代码:
use strict;
use warnings;
my $description="The baseball player's spiritual redemption and recovery from drug addiction.";
if($description =~ /'/)
{
print "I am here\n";
$description =~ s/'/foo/g;
}
print "description = $description\n";
产生以下输出:
I am here
description = The baseball playerfoos spiritual redemption and recovery from drug addiction.