hi,
i'm trying to match lines where parts of the regular expression depend on what's been found earlier, i.e. i have some values that may or may not be enclosed in quotes. if there are quotes, then the values may contain anything but the quotes themselves, if not then they may only contain a limited set of characters. in order to do that, i wanted to use the following code:
use strict;
my %allowed = (
"" => "[\w\.\-]*",
"\"" => "[^\"]*",
"\'" => "[^\']*"
);
my @lines = (
"id=\"abc\"", # expected: abc
"id=\'abc\'", # expected: abc
"id=\"abc def\"", # expected: abc def
"id=abc", # expected: abc
"id=abc def", # expected: abc
);
foreach my $line ( @lines ) {
print $line;
if( $line =~ /\w=([\'\"]?)($allowed{\1})\1(.*)$/ ) {
print " => $2/$3\n";
}
else {
print " didn't match\n";
}
}
i expected the value of the first parenthesized expression ([\'\"]?) to be used to retrieve and insert the collection of allowed characters, but when i run perl it tells me:
pagod> perl test.pl
Quantifier follows nothing in regex; marked by <-- HERE in m/\w=(['"]?)(* <-- HERE )\1(.*)$/ at test.pl line 18.
id="abc"
i've tried to put the '*' in the values in the hashtable, but then $2 is always empty :-\
doesn't it work that way, or have i done anything wrong? any idea how i could do that?
thx in advance!
Pagod
i'm trying to match lines where parts of the regular expression depend on what's been found earlier, i.e. i have some values that may or may not be enclosed in quotes. if there are quotes, then the values may contain anything but the quotes themselves, if not then they may only contain a limited set of characters. in order to do that, i wanted to use the following code:
use strict;
my %allowed = (
"" => "[\w\.\-]*",
"\"" => "[^\"]*",
"\'" => "[^\']*"
);
my @lines = (
"id=\"abc\"", # expected: abc
"id=\'abc\'", # expected: abc
"id=\"abc def\"", # expected: abc def
"id=abc", # expected: abc
"id=abc def", # expected: abc
);
foreach my $line ( @lines ) {
print $line;
if( $line =~ /\w=([\'\"]?)($allowed{\1})\1(.*)$/ ) {
print " => $2/$3\n";
}
else {
print " didn't match\n";
}
}
i expected the value of the first parenthesized expression ([\'\"]?) to be used to retrieve and insert the collection of allowed characters, but when i run perl it tells me:
pagod> perl test.pl
Quantifier follows nothing in regex; marked by <-- HERE in m/\w=(['"]?)(* <-- HERE )\1(.*)$/ at test.pl line 18.
id="abc"
i've tried to put the '*' in the values in the hashtable, but then $2 is always empty :-\
doesn't it work that way, or have i done anything wrong? any idea how i could do that?
thx in advance!
Pagod