in reply to Re: Regex String
in thread Regex String

Hi,

I am searching a hexdump. Just want to make sure it is not all zeroes, before i split it to bytes.

Looks like 00000FFEDFF67FFB8FF96FFE200BBFF240020FFBAFF360132FF6500FCFED30079

Thanks a lot for your reply.

Replies are listed 'Best First'.
Re^3: Regex String
by AppleFritter (Vicar) on Jul 04, 2014 at 09:54 UTC

    You're welcome! And in that case, I'd change those checks to the following:

    die "Not a valid file, Check output!" if $dumpbuf =~ m/[^0-9A-F]/; #ma +tch one die "File contains only zeros, Check output!" if $dumpbuf !~ m/[1-9A-F +]/; #match two

    (In particular, note that the second regex should include A-F, since otherwise some valid files will be rejected.)

      You could also try the following, as an alternative solution:

      die "Invalid file, check output!" unless $dumpbuf =~ /^(?!0*$)[0-9A-F]+$/; # Ensure file contains a hex string other than all 0
Re^3: Regex String
by AnomalousMonk (Archbishop) on Jul 04, 2014 at 14:52 UTC
    ... split it to bytes.

    If this means you want to end up with an array (or string) of byte values for each pair of hex digits, consider pack:

    c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my $s = '00000FFEDFF6'; print qq{'$s'}; ;; my @bytes = split '', pack 'H*', $s; dd \@bytes; " '00000FFEDFF6' ["\0", "\0", "\17", "\xFE", "\xDF", "\xF6"]

    Update: Another validation test to make sure you have an even number of hex digits in the source string might also be a good idea.