People attacking binary data with regular expressions generally have two problems: by default, /./ won't match newlines, and /$/ will, on a string which ends with character 10, match just before the final character.

The solution to the first of these problems is to use the s modifier on your regexp, as has already been mentioned. Note that you can encode this modifier into the regexp when you define it:

my $data = "\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11"; my @find = ( # These Work qr"\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11"s, qr"\x04\x05.{4}\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11"s, qr"\x04\x05\x06\x07\x08.{1}\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11"s, qr"\x04\x05\x06\x07\x08\x09\x0a.{1}\x0c\x0d\x0e\x0f\x10\x11"s, #These didn't work before, but do now. qr"\x04\x05.{5}\x0b\x0c\x0d\x0e\x0f\x10\x11"s, qr"\x04\x05\x06\x07\x08\x09.{1}\x0b\x0c\x0d\x0e\x0f\x10\x11"s, ); foreach (@find) { if ($data =~ /$_/) { print "found\n" } else { print "not found\n" } +; };
The solution to the second problem is to use \Z instead of $ when you absolutely need the very end of the string, even if it ends in a newline.

As for online regexp references, doing perldoc perlre as suggested before is good (if you are working on windows and have ActiveState's perl installed, then search C:\Perl\html\index.html for "perlre") - if you prefer to look at some online page, googling for perlre will get you several online copies of the same page.

--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

In reply to Re: Using regexp with binary data by fizbin
in thread Using regexp with binary data by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.