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

Hi Monks
I have a complicated pattern matching cases which doesn't give me the desired results.
I appreciate your input.
#!/usr/local/bin/perl -w use strict; my $p1 = qr(^.*?\s\-); my $p2 = qr(^.*?Special); my $p3 = qr(^.*?:); my $p4 = qr(^\'.*?\'); foreach (<DATA>){ chomp; m[($p1)|($p2)|($p3)|($p4)]; #print "$_\n"; print "\t","$+\n"; } __DATA__ 'Inside: Single-Quotes' -XYZ the colon after inside 'Also in quotes' and Special Word and -Oops nothing more These are just plain words and dash -X and some data
Now When I run this:
It gives me
        'Inside: Single-Quotes'  -
        'Also in quotes' and Special Word and -
        These are just plain Special words and dash -
I would like to have
Inside
Also in quotes
These are just plain
Thanks
Artist

Replies are listed 'Best First'.
•Re: Another First Pattern Matching
by merlyn (Sage) on Jul 15, 2002 at 19:18 UTC
    The regex alternatives are processed from left to right. Since the \s matches before the quoted one, you get that first. If you don't want that, reshuffle the alternatives.

    -- Randal L. Schwartz, Perl hacker

Re: Another First Pattern Matching
by BUU (Prior) on Jul 15, 2002 at 19:37 UTC
    you might want to try something more along the lines of /'([^'])*?/