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

Hello fellow Monks, Is it possible in a regular expression (or some other way) to match the null character? For an application here at work, I attempted to write a perl "record scrubber" that walks through a record and looks for Null bytes. Unfortunately, it does not match to \000 or \x00; that is to say, given a line like this: HELLO\0\0GOODBYE A C program would find the two nulls between Hello and Goodbye but perl apparently doesn't. Is there a way to do this? here is my code:
while (<INFILE>) { $CurrentLine = $_; $WordPos = 0; if ($Line =~ /\000/) { print "Found nulls at line $Line file position: "; } }

Replies are listed 'Best First'.
Re: Looking for NULLS in a string
by dreadpiratepeter (Priest) on Sep 20, 2002 at 14:43 UTC
    One thing I am noticing right off is that the $Line variable you are matching against does not appear to contain anything useful. I'm hoping that is a transcription error and not a sign (along with the lack of my's) that you are not using strict. Which would have told you about the misnaming of $CurrentLine to $Line.
    If this is indeed a transcription error I apologize and point out that \0 is the proper metacharacter for null in the Perl regular expression (See p163 of the Camel book).
    Hope this helps,

    -pete
    "Pain heals. Chicks dig scars. Glory lasts forever."
Re: Looking for NULLS in a string
by blokhead (Monsignor) on Sep 20, 2002 at 14:40 UTC
    Your code if ($Line =~ /\000/) is searching within the $Line variable, however, I don't think that's what you want. You want to match within $CurrentLine, right? if ($CurrentLine =~ /\000/). However, if all you want to do is find the string offset of a single character, use the index function, which will be much more efficient.

    blokhead

Re: Looking for NULLS in a string
by Zaxo (Archbishop) on Sep 20, 2002 at 16:12 UTC
    while (<INFILE>) { local $" = ', '; my @positions; push @positions, pos() while /\000/g; print 'Found nulls at line ', $. , ', positions ' "@positions.$/" if @positions; }

    $. is the line number maintained by the diamond operator. $" is the record seperator for stringified arrays. pos() is the last match position maintained by the regex engine.

    After Compline,
    Zaxo

Re: Looking for NULLS in a string
by zentara (Cardinal) on Sep 20, 2002 at 16:02 UTC
    I think you just want =~ /\0/ This script will strip out Nulls from a test file.
    #!/usr/bin/perl use warnings; open(IH,"<null-bytes-test-in") or die $!; @in = (<IH>); close IH; open(OH,"+>null-bytes-test-out"); foreach $line (@in){ $line =~ s/\0//g; print OH $line; } close OH;