我是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条件.
你需要这样做:
if($wholeText =~ /"$/)
{
$wholeText =~ s/"(.*?)"/$1/s;
}
Run Code Online (Sandbox Code Playgroud)
.除非您应用/s修饰符,否则与换行符不匹配.
没有必要像你一样逃避引号.
| 归档时间: |
|
| 查看次数: |
15205 次 |
| 最近记录: |