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

Hi Can someone tell me how to write a regular expression to match "téléphone" so it will match in an external file. (Don't worry know how to open a file:) I can't guanantee the encoding of the external file, so I've declared: use utf8; Nothing works so, I think I've really got the wrong end of the stick somewhere. I'd be gratefule for any help. Thanks
  • Comment on Reguar expressions and french characters

Replies are listed 'Best First'.
Re: Reguar expressions and french characters
by moritz (Cardinal) on Aug 28, 2007 at 11:41 UTC
    If you don't know the encoding if the external file you are lost. Binary data without a receipt how to interpret it is rather useless.

    If you know a few possible encodings you can use Encode::Guess, which works fairly well for large files and central european languages.

    Read Encode and perluniintro to understand the basic ideas.

Re: Reguar expressions and french characters
by graff (Chancellor) on Aug 28, 2007 at 23:15 UTC
    (Don't worry know how to open a file:) I can't guanantee the encoding of the external file...

    As moritz points out, if you don't know what encoding is being used in the file, then you don't really know how to open the file.

    If you're actually dealing with a bunch of different files that may be in different encodings, then Encode::Guess is probably the only way to go. But if the input is known to be consistent, maybe you just need to figure out once what the encoding is, and write your script to handle that encoding.

    There really aren't very many different choices when it comes to French; "é" in a single-byte encoding might be either 0x82 (older PC code pages with 3-digit numbers, eg. CP437, CP720, etc), 0x8E (older Mac code pages) or 0xE9 (various iso-8859-n and Windows CP125n).

    In multi-byte form, it's most likely unicode (0xC3 0xa9 in utf8, or 0x00E9 in utf-16 -- but this latter could be either big- or little-endian).

    Just use some sort of hex dump tool on your file to see what it is, and use the proper encoding as an IO layer when opening the file:

    open( my $fh, "<:cp1252", "file.name" ) or die $!; # assuming it's cp +1252
Re: Reguar expressions and french characters
by Joost (Canon) on Aug 28, 2007 at 23:53 UTC
Re: Reguar expressions and french characters
by SFLEX (Chaplain) on Aug 28, 2007 at 12:47 UTC
    Here is a regex that matches "téléphone"
    #!/usr/bin/perl my $string = 'is that a téléphone?'; my $matches = 0; $matches++ if ($string =~ m/téléphone/); # match it & count print "Content-type: text/html\n\n"; print "<html>Current: $string<hr>$matches</html>\n";
    Hope that helps ^^