Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Getting Error in character class - Regex

by prasadbabu (Prior)
on Apr 03, 2006 at 15:33 UTC ( [id://540949]=perlquestion: print w/replies, xml ) Need Help??

prasadbabu has asked for the wisdom of the Perl Monks concerning the following question:

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>front matter comes here</front><body>bodymatter co +mes here</body><back>back matter</back>'; 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

Replies are listed 'Best First'.
Re: Getting Error in character class - Regex
by davidrw (Prior) on Apr 03, 2006 at 15:38 UTC
    it's the array access that's causing the problems.. this works around it:
    my $s = $imlre{$_}->[1]; $input =~ s/<$s[^>]*>//g ;

      Curlies can be used for disambiguation:

      $input =~ s/<${imlre{$_}->[1]}[^>]*>//g ;

      When dealing with HoA, HoH, AoA and AoH, the arrays are optional between the indexes.
      $imlre{$_}->[1]
      is the same thing as
      $imlre{$_}[1]

      When Perl sees
      $imlre{$_}->[1][^>]
      it thinks you mean
      $imlre{$_}->[1]->[^>]
      rather than a HoA followed by a regexp. Between the square brackets, an index is expected. Since ^> is not a valid perl expression, you get an error.

      Update: Rearranged explanation for clarity.

Re: Getting Error in character class - Regex
by holli (Abbot) on Apr 03, 2006 at 15:42 UTC
    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 isn't a valid Perl expression (that should resolve to something numeric).

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

    Update:
    Followed ikegami's nit picking ;)


    holli, /regexed monk/
      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
by murugu (Curate) on Apr 03, 2006 at 15:52 UTC

    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>front matter comes here</front><body>bodymatter co +mes here</body><back>back matter</back>'; 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(;;);

Re: Getting Error in character class - Regex
by codeacrobat (Chaplain) on Apr 03, 2006 at 15:46 UTC
    You have to specify the end of the hashref
    $input =~ s/<${imlre{$_}->[1]}[^>]*>//g
    as there is ambiguity for brackets in the regex.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://540949]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found