On Wed, 21 Nov 2012 20:54:29 -0500
shawn wilson <ag4ve...@gmail.com> wrote:

> how do i capture all quoted strings?

OK, I rewrote it as a simple parser:

DATA_LOOP:
while( my $line = <DATA> ){
  print $line;

  my @captured = ( '' );
  my $inside = 0;

CAPTURE_LOOP:
  while( 1 ){

    if( $line =~ m{ \G ( [^"\\]+ | \\ ["\\] ) }gcx ){
      $captured[-1] .= $1 if $inside;
    }elsif( $line =~ m{ \G \" }gcx ){
      $inside ^= 1;
      push @captured, '' if ! $inside;
    }else{
      last CAPTURE_LOOP;
    }

  }
  pop @captured; # last one is always any empty string


  if( $inside ){
    warn "missing closing quotes\n";
  }

  print "\t$_\n" for @captured;
  print "\n";
}

__DATA__
"something" 444
\"escaped quote\" 321
"\"esc quote\" other stuff" 567
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; . NET
CLR 1.1.4322; msn OptimizedIE8;ESMX)" 556 "\"Mozilla\"" 555


-- 
Just my 0.00000002 million dollars worth,
        Shawn

Programming is as much about organization and communication
as it is about coding.

Why fit in when you can stand out?
        Dr. Seuss

The only way that problems get solved in real life is with a lot of
hard work on getting the details right.
        Linus Torvalds

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to