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

I am not having any luck trying to select telephone number with parenthisis from the record using following command if ($_ =~ /$field/) { print $_; where $field = "(555)555-1234
  • Comment on How do I search strings with parenthisis

Replies are listed 'Best First'.
Re: How do I search strings with parenthisis
by Wassercrats (Initiate) on May 05, 2004 at 15:23 UTC
    You have to quote $field or else the parentheses will act as metacharacters in the regex. Use if ($_ =~ /\Q$field\E/) instead.
      Thks that worked.
Re: How do I search strings with parenthisis
by matija (Priest) on May 05, 2004 at 15:21 UTC
    To match a parenthisis, you need to escape the paren with a backslash:'\('. But since you're using double quotes (which will automaticaly interpolate backslashes), you need to double them:"\\(".
Re: How do I search strings with parenthisis
by davido (Cardinal) on May 05, 2004 at 15:26 UTC
    my $field = "(555)555-1234"; while ( <DATA> ) { print if /\Q$field\E/; } __DATA__ (111)222-3456 (555)555-1234 (666)777-8888


    Dave

Re: How do I search strings with parenthisis
by ambrus (Abbot) on May 05, 2004 at 16:08 UTC