1

I have a program to parse inf files of Windows Printer drivers.

System works fine as long as the file is written ASCII.

So, for exemple, regex m/^\[([^\]\]*)$/ works fine to match sections headers.

In the case of a Unicode line, the line matches m/^\0\[([^\]\0\]*)$/ and certainly not the first. So, file is not correctly parsed.

How to make perl regexes ignore \0 charachters?

1
  • Config::Simple should be able to read INF files. They follow the syntax of INI files. But I am not sure if it reads the things that look like comments correctly. I'd give it a try to avoid this problem all together.
    – simbabque
    Apr 12, 2016 at 12:52

3 Answers 3

1

1) Make sure you have Perl 5.14 or newer.

2) Read the perlre manpage, particularly the section titled "Character set modifiers"

3) Use the /a, /u, /l or /d flag on your regexp depending on which behavior best suits your need.

1
  • The problem is that his text is encoded using UTF-16le. This won't help.
    – ikegami
    Apr 12, 2016 at 17:29
1

You can't just "ignore" null characters. That way any non-ASCII characters in the data won't be interpreted properly

It sounds like you're trying to parse UTF-16BE-encoded data without decoding it. Use the core Encode module to decode it into Perl internal representation, like this

use strict;
use warnings 'all';
use feature 'say';

use Encode;

my $bytes = "\0[\0x\0x\0x\0]";

my $string = decode('UTF-16BE', $bytes, Encode::FB_CROAK);

say $string;

output

[xxx]

If I'm wrong about the encoding then please show a dump of your input string, using

use Data::Dumper;
$Data::Dumper::Useqq = 1;
$Data::Dumper::Terse = 1;

print Dumper $bytes;
0

Always decode your inputs and encode your outputs. Specifically, change

open(my $fh, '<', $qfn)

to

open(my $fh, '<:encoding(UTF-16le)', $qfn)

This will decode the text from bytes into code points, allowing your matches to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.