Нужна помощь с регулярным выражением. Я пытаюсь сопоставить строку с двойными кавычками текста в большой строке, которая сама может содержать пары двойных кавычек! Вот пример:Perl - регулярное выражение для совпадения с двойным кавычками
"Please can ""you"" match this"
Ниже приведен более полный пример моей проблемы и где я до сих пор показан. В приведенном ниже коде хранится «paris» правильно в хэше, как лондон, так и мельбурн неверны из-за пары двойных кавычек, заканчивающей длинное описание раньше.
Любая помощь очень ценится.
use strict;
use warnings;
use Data::Dumper;
my %hash;
my $delimiter = '/begin CITY';
local $/ = $delimiter;
my $top_of_file = <DATA>;
my $records=0;
while(<DATA>) {
my ($section_body) = m{^(.+)/end CITY}ms;
$section_body =~ s{/\*.*?\*/}{}gs; # Remove any comments in string
$section_body =~ m{ ^\s+(.+?) ## Variable name is never whitespace seperated
## Always underscored. Akin to C variable names
\s+(".*?") ## The long description can itself contain
## pairs of double quotes ""like this""
\s+(.+) ## Everything from here can be split on
## whitespace
\s+$
}msx;
$hash{$records}{name} = $1;
$hash{$records}{description} = $2;
my (@data) = split ' ', $3;
@{ $hash{$records} }{qw/ size currency /} = @data;
++$records;
}
print Dumper(\%hash);
__DATA__
Some header information
/begin CITY
london /* city name */
"This is a ""difficult"" string to regex"
big
Sterling
/end CITY
/begin CITY paris
"This is a simple comment to grab."
big
euro /* the address */
/end CITY
/begin CITY
Melbourne
"Another ""hard"" long description to 'match'."
big
Dollar
/end CITY
Непонятно, на каком ожидаемом выходе или даже на вход. – yzxben
@peapls и пример ввода находится в разделе '__DATA__'. – dlamblin
Я думаю, вы должны тестировать это с помощью средства регулярного выражения; НАПРИМЕР. http://www.regexbuddy.com/ или что-то в этом роде. – dlamblin