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

Within a search line is it necassary to escape a colon with the backslash escape character? My script seems to work either way, but I must be sure. Example:  if ($Line =~ m/\/$findname\:/) I am examining the passwd file for various accounts. Thanks

Replies are listed 'Best First'.
Re: Escape Character
by athomason (Curate) on Jul 12, 2000 at 00:48 UTC
    As Adam said, : is not a metacharacter. However, an escaped non-alphanumeric character is equivalent to the character itself, which is why your regexp works both ways. Escaped alphanumerics, on the other hand, have special meanings (\s is whitespace, \t is tab, \1 is first regexp match, etc).
Re: Escape Character (Adam: list of metachars)
by Adam (Vicar) on Jul 12, 2000 at 00:42 UTC
    No. The list of meta characters is (from programming perl):
     \ | ( ) [ { ^ $ * + ? .
    These are the only characters requiring a backslash in a regular expression. I'll add the delimiter / to that list, if its part of your string.

    Disclaimer: Obviously, if you do something like m!!, and then want to find an exclamation mark, then you'll need to escape it. Also, under that circumstance, you won't need to escape a /

    Update: athomason adds a good point, be sure to read the next post too.