Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Using regexp with binary data

by fizbin (Chaplain)
on Aug 28, 2005 at 15:34 UTC ( [id://487264]=note: print w/replies, xml ) Need Help??


in reply to Using regexp with binary data

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@/

Replies are listed 'Best First'.
Re^2: Using regexp with binary data
by Anonymous Monk on Aug 28, 2005 at 16:06 UTC
    Thanks for your help everyone. It is greatly appriciated.


    Its things like qr"\x04\x05.{4}\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11"s that make me really love perl. I mean that is just poetry in programming right there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://487264]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-25 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found