如何在Perl中提取用双引号括起来的子字符串?

dom*_*lao 3 regex perl

我是Perl和正则表达式的新手,我很难提取用双引号括起来的字符串.例如,

"Stackoverflow is

awesome"

在我提取字符串之前,我想检查它是否是变量中整个文本行的结尾:

if($wholeText =~ /\"$/)   #check the last character if " which is the end of the string
{
   $wholeText =~ s/\"(.*)\"/$1/;   #extract the string, removed the quotes
}
Run Code Online (Sandbox Code Playgroud)

我的代码不起作用; 它没有进入if条件.

cha*_*aos 8

你需要这样做:

if($wholeText =~ /"$/)
{
    $wholeText =~ s/"(.*?)"/$1/s;
}
Run Code Online (Sandbox Code Playgroud)

.除非您应用/s修饰符,否则与换行符不匹配.

没有必要像你一样逃避引号.