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

标签 regex perl

我是 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/

相关文章:

Perl Mojolicious + PgAsync 插件——泄漏数据库连接?

perl - grep 匹配某个模式的行,以及匹配前后的行

perl - 在 perl 中使用标量作为条件

JavaScript:重构循环正则表达式

php - 如何在 PHP 中将逗号和 "and"与 preg_split 匹配

JavaScript : Replace Backslash and double Quotes with some other char

regex - 哪个更有效的正则表达式?

perl - 如何避免 Type::Tiny::_build_coercion 中的 "deep recursion"错误/警告

regex - 如何从 Perl 字符串中删除特定字符?

javascript - 动态变量写入正则表达式