Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

matching using backref as hash index

Status
Not open for further replies.

pagod

Programmer
May 8, 2007
3
DE
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
 
BUGFIX: in the original version, there's no '*' quantifier in the values in %allowed, instead the '*' is in the regular expression after $allowed{\1}. that's what gives the error shown above
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top