【问题标题】:Need to pick up everything in double or single quotes from the file in Perl需要从 Perl 中的文件中提取双引号或单引号中的所有内容
【发布时间】:2014-03-18 04:44:21
【问题描述】:

我是 Perl 的新手。有一个文件有多行。所有行在单引号或双引号内都有单词。有时这些话会出现在另一行。例如:

  there is word "apple" inside quotes. Another 'single' quotes 
  second line with 'different' string. Here the double "line 
                                         string"

我用

   while ($value = <$file>) {
   my @array = $str =~ /(['"]+.*?['"]+)/g; 

  }

拿起单词,但我不知道如何在引号中获取两行或多行字符串。我有任何不使用 Perl 模块或内部命令的正则表达式解决方案吗?

所需的输出:

"apple" 
'single' 
'Different'
"line string" 

谢谢

【问题讨论】:

  • 你想做什么,去掉",'和引号内的新行?
  • 我刚刚编辑了我的问题
  • 好的,简单地说,您想要修复已传递到下一行的行,例如您的示例"Line\nstring" \n 这里代表换行符。
  • 是的,但我什至不知道如何接他们

标签: regex perl


【解决方案1】:

试试这个:

#!/usr/bin/perl

use warnings;
use strict;

use Data::Dumper;

local $/;

my $data = <DATA>;

my @strings = map { s/\s*\n\s*/ /; $_ } ( $data =~ m/((?:'[^']+')|(?:"[^"]+"))/sg );

print Dumper(\@strings);


__DATA__
there is word "apple" inside quotes. Another 'single' quotes 
second line with 'different' string. Here the double "line 
string"

【讨论】:

    猜你喜欢
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2016-02-02
    • 1970-01-01
    • 2010-10-30
    • 2013-11-09
    相关资源
    最近更新 更多