david.paige has asked for the wisdom of the Perl Monks concerning the following question:

I am having a problem maintaining some perl code. On ActivePerl on Windows, this bit of code works fine; on Solaris 10 5.6.1 it doesn't.

The code searches for terms in a file, and changes the color if it finds them. For example, to find the (C) string, I use
\( *[Cc] *\)
which should look for an opening parenthesis, any number of spaces, one case-insensitive C, any number of spaces, and a closing parenthesis.

In windows, it correctly highlights the first three characters of "(C)opyright", but it Solaris it finds multiple "C", not just in parenthesis.

foreach $WORD in (@WORD_LIST) { $LINE =~ s/($WORD)/<font color=red><b><i>$1<\/i><\/b><\/font>/gi & +& $contains_word++; }
I can't figure out how Solaris is different. It only triggers on the "C", not the "(C)", making my output file much bigger than it should be. Thanks for the assistance. (substitution at paiged.fea.st)

Replies are listed 'Best First'.
Re: Substitution Problem
by kennethk (Abbot) on Mar 25, 2009 at 18:37 UTC
    It sounds like an interpolation issue to me, i.e. on your Solaris machine you are actually running the match against ( *[Cc] *), not \( *[Cc] *\). Are you actually running the same script file on the two machines? Can you show us your assignment statement (copy/paste) for @WORD_LIST? Neither the regex syntax nor the String interpolation syntax should vary between the two versions of perl.
      Bingo. It was a problem with the word list file. When I did a more thorough examination, I didn't have the "\" in the search term. Adding them made the search work correctly. It was a very vexing problem, as I could see no reason that it should parse differently. Thank you for the assistance. Status: Resolved.
        If the search term are to be taken literally, change
        s/($WORD)/...
        to
        s/(\Q$WORD\E)/...
Re: Substitution Problem
by RoyCrowder (Monk) on Mar 25, 2009 at 18:35 UTC
    In your regex:
    $LINE =~ s/($WORD)/<font color=red><b><i>$1<\/i><\/b><\/font>/gi
    You are using ($WORD). This will consider the () to be a group. If you are wanting to find the () as well as the $WORD you need to try this:
    $LINE =~ s/\(($WORD)\)/<font color=red><b><i>$1<\/i><\/b><\/font>/gi