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

I have a .txt file that needs to parse out everything within quotation including underscore. I know this is an easy task but Im a Perl noobie... Below is what the file looks like:
rmdir: directory "SAS_util0001000021A9_sasux101": Directory rmdir: directory "SAS_util000100002E19_sasux101": Directory rmdir: directory "SAS_util000100005891_sasux101": Directory Heres the code I used but its wrong, I need to extract everything with +in the quotes into a variable or an array. while ($line = <CANT2>) { if ($line =~ /[\"\w+\_w+\_w+\"]/) { $keep = $1; push(@test,$keep); } }
I couldnt use substring because some lines are not aligned with most of the lines. Thanks in advance... Joe

Replies are listed 'Best First'.
Re: Reg Ex with quotes
by GrandFather (Saint) on Jan 30, 2006 at 23:52 UTC

    Your problem is that you have used a char set where you intended a capture. The fixed (and simplified) regex looks like /("[^"]*")/.


    DWIM is Perl's answer to Gödel
      /"([^"]*)"/
      is probably even better, unless you really do want the quotes captured too. The value of the capture will be available in $1.

        That is what I would do, but doesn't match the inferred intent of OP's code. (Mind you, my version doesn't do the checking that the original code did either. :) )


        DWIM is Perl's answer to Gödel
      Looks great but I get a compilation error with the reg ex.. Thanks Joe..
Re: Reg Ex with quotes
by Fletch (Bishop) on Jan 30, 2006 at 23:52 UTC
    /directory "(.*?)": Directory/

    perlre and look for non-greedy quantifiers.

      I only need the information inside the quotes. I dont need anything else outside the code.. Thanks again Joe...

        Then you obviously still need to read perlre because that only captures the part in quotes.

Re: Reg Ex with quotes
by smokemachine (Hermit) on Jan 31, 2006 at 02:41 UTC
    perl -ne 'push(@array,$1) if /"(.*?)"/' data_file