我是 Perl 和正则表达式的新手,我很难提取由双引号括起来的字符串。例如,
"Stackoverflow is awesome"
Before I extract the strings, I want to check if it is the end of the line of the whole text was in the variable:
if($wholeText =~ /\"$/) #check the last character if " which is the end of the string
{
$wholeText =~ s/\"(.*)\"/$1/; #extract the string, removed the quotes
}
我的代码不起作用;它没有进入
if
健康)状况。
最佳答案
你需要做:
if($wholeText =~ /"$/)
{
$wholeText =~ s/"(.*?)"/$1/s;
}
.
除非您应用 /s
,否则不匹配换行符修饰符。没有必要像你做的那样逃避引号。
关于regex - 如何在 Perl 中提取用双引号括起来的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1485046/