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

Aologies for this being such a basic question, but probably people have done this many times and I'm bad & slow at regexes. I'm maintaining a perl script without knowing enough perl myself (yet) so a solution would be much appreciated.

The script splits two filenames out of a line of text; now it needs to handle long filenames within quotes, where a space was previously the delimiter.
Many thanks
Alan

$TheLine = "REFERENCE z:/bin/JAnalyzer/fred.lst package/fred.lst"; $TheLine =~ /(\S*)\s+(\S*)\s+(\S*)\s*/; print "1 is $1 and 2 is $2 and 3 is $3\n"; $TheLine = 'REFERENCE "z:/bin/JAnalyzer/Justice Analyzer Users Guide.p +df" "package/Justice Analyzer Users Guide.pdf"'; $TheLine =~ /(\S*)\s+(\S*)\s+(\S*)\s*/; print "1 is $1 and 2 is $2 and 3 is $3\n"; #hmmm

Replies are listed 'Best First'.
Re: newbie parsing question
by lidden (Curate) on Aug 28, 2004 at 17:12 UTC
    [^"]+matches one or more of chars that is not '"'.
    $TheLine = 'REFERENCE "z:/bin/JAnalyzer/Justice Analyzer Users Guide.p +df" "package/Justice Analyzer Users Guide.pdf"'; $TheLine =~ /(\S*)\s+"([^"]+)"\s+"([^"]+)"\s*/; print "1 is $1 and 2 is $2 and 3 is $3\n";
    Does this do what you want?
Re: newbie parsing question
by wfsp (Abbot) on Aug 28, 2004 at 17:08 UTC
Re: newbie parsing question
by bobf (Monsignor) on Aug 28, 2004 at 19:38 UTC

    The replies above will help you maintain the code as it is, and pull the filenames out of the text. If you plan to do path or filename manipulation, however, I'd suggest taking a look at the File:: modules (specifically File::Spec and File::Path).

    Best of luck.
Re: newbie parsing question
by CountZero (Bishop) on Aug 28, 2004 at 19:52 UTC
    A more general solution (works with any number of filenames in your input string):
    use strict; my $TheLine = 'REFERENCE "z:/bin/JAnalyzer/Justice Analyzer Users Guid +e.pdf" "package/Justice Analyzer Users Guide.pdf"'; my @Files= $TheLine=~m/"([^"]*?)"/g; print join "\n", @Files;
    If you want to be really neat you could replace the [^"] by the list of characters valid in filenames.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: newbie parsing question - thanks all!
by anadem (Scribe) on Aug 29, 2004 at 01:29 UTC
    many thanks to all for the help.
    Lidden's solution is the right fix for now, and i'll read up on File::module plus some more basic perl learning :-)