in reply to Regular Expression problem with $variable =~ m/C++/

The argument of the match operator (m//) is a regexp, not arbitrary text. An easy way to convert arbitrary text into a regular expressions in to use quotemeta.
my $re = quotemeta($VarString); next unless $Array[$Counter] =~ m/$re/;

Another trick is to use \Q...\E.

next unless $Array[$Counter] =~ m/\Q$VarString\E/;

By the way, do realize that if $VarString = 'C';, (poorly named) $Array[0] would match because there's a C in VISUAL BASIC? You also have problems with $VarString = 'VB'; (doesn't exist) and $VarString = 'Perl'; (case issue)

Replies are listed 'Best First'.
Re^2: Regular Expression problem with $variable =~ m/C++/
by mtar (Novice) on Jun 12, 2007 at 12:37 UTC
    Thank you, 'quotemeta' is a very good solution. I have also tried already the use of \Q...\E.

    You are right when you speak (for example) about 'c' letter in Visual Basic. Fortunately I have to compare always complex names that are never included in other name.
    Thank you for your support!
      You might find the following useful:
      my @matches = grep { ",$Array[$_]," =~ /,\Q$VarString\E,/i } 0..$#Arra +y;

      @matches will contain the indexes (0, 1, 2 and/or 3) or the elements of @Array that contains the matching substrings. The commas ensure C won't match VISUAL BASIC, and the i ensures Perl will match PERL.

      If the same item won't appear in two array elements, then you can use the following version to store the index in $match:

      my ($match) = grep { ",$Array[$_]," =~ /,\Q$VarString\E,/i } 0..$#Arra +y;