Home: Perl Programming Help: Regular Expressions:
Unrecognized escape \M passed through in regex



dareno
New User

Jul 28, 2009, 3:35 AM


Views: 8170
Unrecognized escape \M passed through in regex

I'm new to Perl, I'm using ActivePerl v5.10.0 and Komodo Edit, running on windows.
My code is:

Code
#!/usr/bin/perl -w 
#use strict;

my $filename = "C:\\Documents and Settings\\pie\\Moje dokumenty\\perl\\dictionary.txt";
my $word = "fiche";

open $filehandle, $filename or die "I couldn't get file: $!";

while ($line = <$filehandle>) {
if ($line =~ /$filename/) {
print $line;
last;
}
}


And the error is: Unrecognized escape \M passed through in regex; marked by <-- HERE in m/C:\Documents and Settings\pie\M <-- HERE oje dokumenty\perl\dictionary.txt/ at C:\Documents and Settings\pie\Moje dokumenty\perl\dict.pl line 10, <$filehandle> line 1.
Could you help?


FishMonger
Veteran / Moderator

Jul 28, 2009, 4:18 AM


Views: 8168
Re: [dareno] Unrecognized escape \M passed through in regex

Use forward slashes instead of backslashes.

Code
my $filename = 'C:/Documents and Settings/pie/Moje dokumenty/perl/dictionary.txt';



dareno
New User

Jul 28, 2009, 5:01 AM


Views: 8166
Re: [FishMonger] Unrecognized escape \M passed through in regex

Thanks. it works.


shawnhcorey
Enthusiast


Jul 28, 2009, 5:33 AM


Views: 8163
Re: [dareno] Unrecognized escape \M passed through in regex


In Reply To

Code
if ($line =~ /$filename/) {


And the error is: Unrecognized escape \M passed through in regex; marked by <-- HERE in m/C:\Documents and Settings\pie\M <-- HERE oje dokumenty\perl\dictionary.txt/ at C:\Documents and Settings\pie\Moje dokumenty\perl\dict.pl line 10, <$filehandle> line 1.
Could you help?


You need to disable the meta-characters in $filename when you do the match.

Code
if ($line =~ /\Q$filename\E/) {


See `perldoc perlre` and search for /\\Q/

Also see `perldoc perlretut`

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".