in reply to Regex String

While searching for multiple patterns on the same string, search continues from previous successful match point in the string. Is this expected ? How to make each pattern search start from beginning of the string each time.

Yes, this is a feature; the /g modifier in scalar context means "start matching where I last left off". From perlretut:

The modifier //g stands for global matching and allows the matching operator to match within a string as many times as possible. In scalar context, successive invocations against a string will have //g jump from match to match, keeping track of position in the string as it goes along.

So the solution is to simply remove that modifier from the first two regexen, the ones that check if the file's a valid one. Alternatively, you could reset the position by using pos (which returns an lvalue, so you can assign to it).

That said, I'm taking a stab in the dark here as to what you want to accomplish. If this isn't it, could you be so kind and give us some sample data, along with the expected output you expect? In particular, I'm curious whether the file is supposed to contain only numbers; the "all zeros" check makes me think it is, but the first check does not ensure this.

P.S. please don't use line numbers in your code -- it just makes it more difficult to download/copy and paste.

Replies are listed 'Best First'.
Re^2: Regex String
by egal (Initiate) on Jul 04, 2014 at 09:47 UTC

    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.

      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
      ... 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.