Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Null characters with Pattern matching doesn't work?

Status
Not open for further replies.

gamesthreshold

Programmer
Jul 22, 2006
4
US


# This one works:
my $lnbk = "\r\n";
$var =~ m{\G(.*?)$lnbk}gc

# This one fails:
my $null = chr(0);
$var =~ m{\G(.*?)$null}gc


Why won't the regex work with a Null character? But this does work...

s/(.*)$null//g


I don't want to use the replacement, but "walk" the variable as I have been doing. Can anyone help?
 
Not sure what you are trying to do or why but I wrote a little code that uses a null character field separator. I could have used "split" but I tried to stay with what you were doing.
Note that I actually embedded null bytes in the data at the end:
Code:
#!/usr/bin/perl
use warnings;
use strict;

my $count = 0;
while(<DATA>) {
    chomp;
    while(
            /                   # Start of regex
                \G              # 'pos' assertion (continue where last left off)                (               # Capture
                    [^\x{00}]*  # any NON-NULL characters (important!)
                )               # End capture
                \x{00}          # The trailing null character
            /gx                 # regex switches
        ) {
        $count++; # Counter just to prove it's going through the loop
        print "[$1] [$count]\n";
    }
}

__DATA__
one^@two^@three^@four^@
five^@six^@seven^@eight^@

Maybe that might give you some ideas....


Trojan.
 
lmfao I made a mistake in my code (shoulda just posted the code in the first place...)

Code:
# Incomming TCP data
sub proto_read_tcp {
   my $sock = shift;
   my $nlen = shift;
   
   return unless $nlen; # Verify new data has been added to the buffer
   
   alias $sock{$sock}->{ibfr}, my $ibfr;
   
   pos $ibfr = 0;
   WHILE:
   while (pos $ibfr < length $ibfr) {
      if ( $ibfr =~ m{\G(.*?)$pactbreak}gcso ) {
         proto_parse_packet($sock, $1);
      } else { # No more packet break's found
         substr($ibfr, 0, pos $ibfr) = '' # clear data we've processed
         print("no more packet breaks... ibfr: $ibfr\n");
         last WHILE; # Break from the loop
      }
   }
}

Well, the problem was that I forgot the "s" for the RegEx options, which makes the . match any character (as it doesn't match line breaks, nulls, etc by default) And I had a linebreak at the beginning of the test packets being sent between my sockets.

The contents of $pactbreak is a null character, so I assumed it was my problem. Didn't realise it was the linebreak at the beginning of the data that was cancelling out the (.*?) part of the search

THX FOR THE HELP THO!! Really appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top