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

I have a very basic regex matching question. If the regex expression ($item in this case below) contains a metacharacter, how can I avoid the run time regex error that one gets when such a metachar is encountered in the expression? e.g. $item = "*varName"; Here is the src line where it blows: if ($func_def =~ /$item/){... I know using a backslash to escape them is one solution but $item will be set programatically on the fly. Are there some special regex options that allow for such metachars to be present in the regex expression and for the matching to take place successfully? Thnx.

Replies are listed 'Best First'.
Re: Metacharacters in regex expression
by Eimi Metamorphoumai (Deacon) on Sep 15, 2004 at 18:08 UTC
    You can either use $item = quotemeta "*varName" or later match against /\Q$item\E/
Re: Metacharacters in regex expression
by sgifford (Prior) on Sep 15, 2004 at 20:08 UTC
    If you always want $item to be treated like a literal string with no regex special characters, use the index function instead of a regex match:
    my $func_def = "char *strcmp(const char *s1, const char *s2)"; foreach my $i ('char *','const char.*','*') { if (index($func_def,$i)) { print "'$func_def' contains '$i'\n"; } }
    Update: ishnid is right; I didn't read the index documentation closely enough. Use his code instead. :) Thanks ishnid!
      Though I'd agree that index could be used, testing index's return value for truth is only useful in some circumstances. If $i isn't contained in $func_def, index returns -1, which is a true value, so your code will think it's been found. Similarly, if $i is at the start of the string (as the first example in yours is), index returns 0 (its position in the string), which is a false value.

      In order to test if one string contains the other, you need to check that index returns >= 0.

      Amended Code:
      my $func_def = "char *strcmp(const char *s1, const char *s2)"; foreach my $i ('char *','const char.*','*') { if ( index( $func_def, $i ) >= 0 ) { print "'$func_def' contains '$i'\n"; } }

        A simple expedient for deriving a boolean value from index is to use 1+index(...). If the value is not found, you get 0. If it is found at the start of the string, you get 1.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Metacharacters in regex expression
by Fletch (Bishop) on Sep 15, 2004 at 19:31 UTC

    "regex expression"? Is that anything like an "ATM machine" or a "PIN number" (or a "rice paddy")? :)