Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

matching quotes

by Eugene (Scribe)
on May 23, 2000 at 08:18 UTC ( [id://14335]=perlquestion: print w/replies, xml ) Need Help??

Eugene has asked for the wisdom of the Perl Monks concerning the following question:

I am looking for a regex that matches quotes. If my string looks like "string \" more strings" it should match the entire thing ignoring the escaped quote.
The one that I got is
/("[^\"]*(\\"?[^\"]+)*")/

for some reason it ignores the escape character and stops there.

Replies are listed 'Best First'.
Re: matching quotes
by perlmonkey (Hermit) on May 23, 2000 at 09:06 UTC
    see matching comments, It has detailed code to do exactly this. Just replace the comments flags $start and $end with '"' for both of them.

    This (slightly modified from merlyn's answer here) will work and is really fast:
    open(FILE, "test.txt") || die; { local $/ = undef; #set to 'slurp' mode $file = <FILE>; #read entire file into $file } close FILE; $start = '"'; $end = '"'; my $inside = 0; my $oldpos = 0; while ($file =~ /(?<!\\)(\Q$start\E|\Q$end\E)/g) { if ($inside == 0) { $oldpos = pos($file) - length($start); $inside++; } else { print substr($file, $oldpos, pos($file)-$oldpos); $inside--; } }
      I was thinking if there is a single regex solution?
        #!/usr/bin/perl -wl use strict; my $string = 'foo "A small \"dog\" eats pie." bar "foobar"'; print $1 while $string =~ /(?<!\\)"(.*?)(?<!\\)"/gs;

        Addendum:

        Hmm. It didn't take me fifteen minutes to write this post. perlmonkey must be typing at relativistic speeds. btw--double quotes are not special in a regex; you don't need to backslash them.

        Here is a single regex inside the while loop, but it is slower:
        while ( $file =~ /((?<!\\)\".*?(?<!\\)\")/sg) { print $1; }
        Update: takshaka is correct of course. I flaked out and did not need to backslach the ", fortunatley it works just fine either way, just more clutter.

        I dont know about my relativistic typing. I guess I am just that good :) Acutally I once took a typing test and got around a -400 wpm (note the negative!) ... apparently I got docked for extensively using the backspace key. (Either that or I typed so damn fast that I overflowed the signed int ... yah, thats it, I really typed 65136 wpm, man I'm good!)
RE: matching quotes
by Russ (Deacon) on May 23, 2000 at 23:18 UTC
    It's not just a regex, but check out Text::Balanced from CPAN.

    Russ

matching variables
by Anonymous Monk on Aug 09, 2000 at 01:53 UTC
    $str="ab:cd:ef:gh";
    print $_."\n";
    while (<>) {
    $find=$_;
    $temp=$str=~/$find+/;
    # this line should return null if I type abc (because
    # string does not contain abc, it contains ab:c) but it
    # returns 1 even if I type aXYZ
    # it returns null only when none of the characters I've
    # typed are in the string...)
    # why is this happening?
    if ($temp==null) {$temp="null";}
    print $temp."\n";
    }
    plz e-mail answer to igor@photomania.net
Re: matching quotes
by Ted Nitz (Chaplain) on May 23, 2000 at 21:36 UTC
    Don't you need to use \\ to get a \ within a charchter class? I'm pretty sure that \ is a meta charchter within a charchter class, otherwise you couldn't do something like [\[\]] to match either [ or ]. Then again I could be wrong. I have a regex at home that does exactly what you need, I'll post it when I get there.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-28 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found