Getting Error in character class - Regex
prasadbabu
created: 2006-04-03 11:33:14

Monks, i have some question in regex. Today when i was doing simple clean up work in xml file, i got the following error. But i was surprised to see that i don't find any error in the code. When i enclosed that character class in the bracket, i am not getting any error. Below i have placed a part of the code and sample input string. This is the first time i am facing such error. Is it my mistake in the code or some other issue behind it? Could anyone explain where am i going wrong.

The error i am getting:

syntax error at D:\testing\pract.pl line 14, near "[^"
Execution of D:\testing\pract.pl aborted due to compilation errors.

use strict;
use warnings;

my %imlre = ('RE1' => ['RE1', 'body', 'measure', 'remove'], 'RE2' => ['RE2', 'jcode', '', 'remove']);

my $input = 'front matter comes herebodymatter comes hereback matter';

for (keys %imlre)
{

	if ($imlre{$_}->[3] =~ /^remove$/i)
	{
		$input =~ s/<$imlre{$_}->[1][^>]*>//g ; #here getting error
		#$input =~ s/<$imlre{$_}->[1]([^>]*)>//g ; #here no error
	}
}

print $input;

Prasad

Re: Getting Error in character class - Regex
created: 2006-04-03 11:38:08
it's the array access that's causing the problems.. this works around it:
  my $s = $imlre{$_}->[1];
  $input =~ s/<$s[^>]*>//g ;
Re^2: Getting Error in character class - Regex
created: 2006-04-03 11:40:19
or
$input =~ s/<${imlre{$_}->[1]}[^>]*>//g ;
Re: Getting Error in character class - Regex
created: 2006-04-03 11:42:30
That's because perl parses the part ${imlre{$_}->[1][^>] as ${imlre{$_}->[1]->[^>], hence an attempt to access an array of arrays. This of course cannot work because "^>" is not numeric.

You must resolve this abiguity eg like you did in s/<$imlre{$_}->[1]([^>]*)>//g. Here the () help the perl parser to DWIM.


holli, /regexed monk/
Re^2: Getting Error in character class - Regex
created: 2006-04-03 11:50:01
It doesn't have to be numeric. Just a valid Perl expression. ^[ is not a valid Perl expression.
Re: Getting Error in character class - Regex
created: 2006-04-03 11:46:11
You have to specify the end of the hashref $input =~ s/<${imlre{$_}->[1]}[^>]*>//g as there is ambiguity for brackets in the regex.
Re: Getting Error in character class - Regex
created: 2006-04-03 11:52:19

Adding a space between two [] worked fine for me. This may be due to the fact that perl tries to parse the second '[]' as extra dimension.

Monks, if wrong please correct me.

use strict;
use warnings;

my %imlre = ('RE1' => ['RE1', 'body', 'measure', 'remove'], 'RE2' => [
+'RE2', 'jcode', '', 'remove']);

my $input = 'front matter comes herebodymatter co
+mes hereback matter';

for (keys %imlre)
{

    if ($imlre{$_}->[3] =~ /^remove$/i)
    {
        $input =~ s/<$imlre{$_}->[1] [^>]*>//xg ; #here getting error
        #$input =~ s/<$imlre{$_}->[1]([^>]*)>//g ; #here no error
    }
}

print $input;

Regards,
Murugesan Kandasamy
use perl for(;;);

perlmonks.org content © perlmonks.org and codeacrobat, davidrw, holli, ikegami, murugu, prasadbabu

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03