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

The requirement string to be matched is:
/lawyers/*(not LDS)*~Firms.html

the pattern I'm using is:
(\/lawyers\/)(.*)(?!LDS)(.*)(\~Firms\.html)+

But it's not working as it matches this sample string
$test = "/lawyers/c~p~~carmel~family~divorce~lds~y~firms.html"; if ($test =~ /(\/lawyers\/)(.*)(?!LDS)(.*)(\~Firms\.html)+/i ){ print "matched\n"; }
Please HELP !!!

Replies are listed 'Best First'.
Re: Pattern matching
by davido (Cardinal) on Dec 29, 2004 at 05:44 UTC

    This is untested tested (see below)...

    m#/lawyers/(?!.*LDS).*~Firms\.html#

    But as perlre states, "Sometimes it's still easier just to say: if (/bar/ && $` !~ /foo$/) ."

    Updated: Switched m!! to m##.

    Update: Here's a test. I think I captured the intent behind your question:

    use strict; use warnings; my @strings = ( '/lawyers/stuffLDSstuff/~Firms.html', '/lawyers/stuffnotstuff/~Firms.html' ); foreach my $string ( @strings ) { print $string; if( $string =~ m#/lawyers/(?!.*LDS).*~Firms\.html# ) { print " matches.\n"; } else { print " doesn't match.\n"; } }

    Dave

      Just want to point out the reg will also exclude those with "LDS" after the "html", e.g.:
      /lawyers/stuffnotstuff/~Firms.html?param=LDS
      I confess I can't think of a way to solve that.

        You can do that by combining the two .* aspects of the pattern:

        m{ /lawyers/ (?: (?!LDS) . )* ~Firms.html }x

        Hugo

      Thanks the answer was of great help
Re: Pattern matching
by gopalr (Priest) on Dec 29, 2004 at 06:19 UTC

    Hi Bedanta,

    I think u r not testing properly.

    It's working fine!!!

    Thanks

    Gopal.R

      The code was not working as it was matching patterns with "lds" in them as well
        it was matching because you used .* as a greedy match, which swallowed up any string including "lds", then (correctly) matched following characters as not containing "lds" etc. try changing the greedy .* to the non-greedy version .*?
        the hardest line to type correctly is: stty erase ^H