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

Re: Regex error when [] occurs in file..

by Joost (Canon)
on Mar 03, 2008 at 15:42 UTC ( [id://671667]=note: print w/replies, xml ) Need Help??


in reply to Regex error when [] occurs in file..

$temp =~ s/#$'//; I'm not even sure what you want that to do, but it constructs a regex out of # followed by whatever was followed by the previous match. Any regex special characters in that generated string will be interpreted as regex directives.

You probably want something like:

while (chomp(my $temp=<INPUT>)){ print Dumper $temp; $temp =~ s/#.*//; # remove comments if ($temp =~ /^\s*$/){ next; } print "after regex:\n"; print Dumper $temp; print "end\n"; }

Replies are listed 'Best First'.
Re^2: Regex error when [] occurs in file..
by why_bird (Pilgrim) on Mar 03, 2008 at 16:03 UTC
    Thanks both---I didn't realise that $' was treated like that. Are all 'special variables' ($&, $N (N is integer)) expanded in that way too? So if you did something like:
    $temp =~ m/(\[0-9\])blah$1/;
    would you match
    $temp = "[0-9]blah6";
    rather than
    $temp = "[0-9]blah[0-9]";
    ?
    V. interesting---I assumed that special characters inside the rest of the data would be ignored..
    Cheers
    <--edit to make the last sentence make more sense!-->
    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......
      $temp =~ m/(\[0-9\])blah$1/;

      I think you meant

      $temp =~ m/(\[0-9\])blah\1/;

      in which case any special characters in the content of the backreference \1 would not be treated special. IOW, "[0-9]blah[0-9]" would match, but not "[0-9]blah6":

      #!/usr/bin/perl use strict; use warnings; for my $temp ("[0-9]blah[0-9]", "[0-9]blah6") { printf "%-15s ", $temp; if ($temp =~ /(\[0-9\])blah\1/) { print "matched\n"; } else { print "didn't match\n"; } }

      prints

      [0-9]blah[0-9] matched [0-9]blah6 didn't match

      while, if you replace \1 with $1 in the above regex, it prints

      Use of uninitialized value in concatenation (.) or string at ./671663. +pl line 8. [0-9]blah[0-9] matched [0-9]blah6 matched

      This is because $1 isn't defined here, thus the regex effectively becomes /(\[0-9\])blah/...

      Update: added demo code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found