【发布时间】:2014-05-10 09:34:24
【问题描述】:
我是 Perl 和正则表达式的新手,我需要从文本文件中提取所有字符串。字符串由用双引号括起来的任何内容来标识。
字符串示例:
"This is string"
"1!=2"
"This is \"string\""
"string1"."string2"
"S
t
r
i
n
g"
代码:
my $fh;
open($fh,'<','text.txt') or die "$!";
undef $/;
my $text = <$fh>;
my @strings = m/".*"/g; # this returns the most out "" in example 4
my @strings2 = m/"[^"]*"/g #fixed the above issue but does not take in example 3
已编辑:我想要 (1) 一个双引号,然后是 (2) 零次或多次出现的非双引号非反斜杠或反斜杠后跟任何字符,然后是 (3)双引号。 (2) 可以是除 "
下面提供的正则表达式 m/"(?:\.|[^"])*"/g 但是当有一行 "string1".string2."string2" 它将返回 "string1" string2 "string3"
有什么地方可以跳过之前匹配的单词吗?
有人可以帮忙吗?
【问题讨论】:
-
请注意,您想要的是 (1) 双引号,然后是 (2) 零次或多次出现的非双引号非反斜杠或反斜杠后跟任何字符,后跟 (3) 双引号。