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

Hey all,

In the code below, I'm trying to set up two different array, (@testtype and @testsuite), well the problem is "Base Functionality" is ending up in the testtype array and not the testsuite array (it's matching 'Functionality' in the regexp). I want these to be exact matches and not partial matches.

I've tried using "^ and $" in the parenthesis and outside but nothing seems to work. I'm sure this is a easy fix, I just can't seem to get a handle on it.

Thank in advance.

if ( $q->param($_) ne "" ) { if ( /(Smoke|Integration|Functional|Non-Functional|Regressio +n|New)/ ) { push (@testtype, $param) if ( $q->param($_) eq "yes" ); } elsif ( /(Base Functionality|\(4\) DPC|Boot Support|Clones|L +ow Priority|Clusters|Data Mobility|Host Based Applications|Performanc +e|Scalability)/ ) { push (@testsuite, $param) if ( $q->param($_) eq "yes" ); } else { $myhash{$param} = $q->param($_); $myhash{$param} =~ s/^\s+|\s+$//g; } }

Replies are listed 'Best First'.
Re^2: Regular Expression Question - Having problems with anchors
by sauoq (Abbot) on Sep 30, 2005 at 18:30 UTC

    You are pushing $param onto your array but you are testing $_ in your regular expressions. As there isn't anyway to tell from the code you've posted what is actually in $param, it's hard to give advice. I can tell you that putting your anchors outside your parens will give you the behavior you want from your regex. (i.e. /^(a|b|c)$/ will match only the exact strings: 'a' or 'b' or 'c' update: or any of those strings with a newline appended.)

    -sauoq
    "My two cents aren't worth a dime.";
    
Re^2: Regular Expression Question - Having problems with anchors
by philcrow (Priest) on Sep 30, 2005 at 18:27 UTC
    This is because 'Base Functionality' includes 'Functional' which is in the first list. You could reorder the if and elsif tests, but other problems of a similar kind are likely occur later. Instead you can anchor on word boundaries with \b, as in ...|\bFunctional\b|...

    Phil

      You can't tell that from the code provided and it is quite possibly not the issue given that he claims he tried anchors outside the parens.

      -sauoq
      "My two cents aren't worth a dime.";