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

Is there a way to search a binary file using a pattern? Thanks!

Replies are listed 'Best First'.
Re: search binary file
by toolic (Bishop) on Jul 11, 2008 at 20:17 UTC
      data is like:
      "my data^A77789^B^Bmore data"; i tried to match it using "/(data^A)(\d+)^B/", i would expect:
      $1 = data^A
      $2 = 77789
      however, it didn't work. (note: ^A is one character)
        By ^A do you mean Control-A (i.e. chr(1)?

        If so, use this:

        if ($input =~ m/(data\x1)(\d+)\x2/) { print "1 = $1, length = ", length($1), "\n"; print "2 = $2\n"; }
Re: search binary file
by pc88mxer (Vicar) on Jul 11, 2008 at 20:08 UTC
    You probably can use regular expressions - perl strings can hold binary data as well as text.

    What is the pattern?