Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Escaping parentheses in regexps

by Tanalis (Curate)
on Jan 22, 2003 at 14:43 UTC ( #229024=perlquestion: print w/replies, xml ) Need Help??
Tanalis has asked for the wisdom of the Perl Monks concerning the following question:


I have a set of strings (pulled from a DB) that I'm concatenating into one string, using the following code:

if ($concat !~ /$string/) { $concat .= " and $string"; }
Simple enough, and it does the job, except where the imported strings contain any form of parentheses, for example () or [], where the string always gets appended regardless of whether it's already there or not.

I'm trying to find a way to escape the strings that are being matched against - does anyone know of a way to do this? I've tried running the string through qq, and tried using /"$string"/ to match, with no success ..

Thanks in advance for your help.
-- Foxcub

Replies are listed 'Best First'.
Re: Escaping parentheses in regexps
by broquaint (Abbot) on Jan 22, 2003 at 14:54 UTC
    Make use of the either quotemeta or the regex equivalent \Q (see. perlre for more info on \Q). So something like
    if ($concat !~ /\Q$string/) { $concat .= " and $string"; }



      And, if you are putting other stuff in your regex that you don't want escaped, end the quotemeta with a \E, ie,
      if ($concat !~ /other stuff\Q$string\E more other stuff/) { $concat .= " and $string"; }

      Project coordinator of the Generic Model Organism Database Project

Re: Escaping parentheses in regexps
by perlguy (Deacon) on Jan 22, 2003 at 15:35 UTC
    You could try using the index function. Then you won't have to worry about escaping characters (at least in this scenario):
    my $concat = "good times and fun (times) and bad (times)"; for my $string ('good times', 'bad times', 'fun (times)') { # if the index function returns -1, it is not a substring if (index($concat, $string) == -1) { $concat .= " and $string"; } } print $concat . "\n";
Re: Escaping parentheses in regexps
by Aristotle (Chancellor) on Jan 22, 2003 at 16:01 UTC
    I wouldn't use a regex at all. Even with escaped metachars that can have surprising results if your database entries contain " and ".
    my %seen; my $concat = join " and ", grep !$seen{$_}++, @db_string;

    Makeshifts last the longest.

Re: Escaping parentheses in regexps
by scain (Curate) on Jan 22, 2003 at 15:45 UTC
      Can you do a "SELECT DISTINCT"

      In general, this is a bad thing to do. select distinct is a Red Flag. It may mean that you are making the database engine fetch a far larger result set than necessary. After it has fetched all that, you then tell it to filter the results and discard the duplicates. A much better approach would be to write the query correctly so that the distinct modifier becomes unnecessary.

      It may mean that you that you need to consider a column that is not referenced specifically in the record set. The workaround may involve using a subselect, which is not available on databases like MySQL. In that case you are out of luck. Nonetheless, your first reflex should be to try and restructure your query. select distinct should be used only as a last resort.

      erm, the "you" doesn't refer to scain here, it refers to you :)

      print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
        Thanks grinder; that is a good point. If you are doing a select distinct often (which, if you are putting it in code, you probably are), you probably do want to rethink your query. You may even find that in order to rethink your query, you may need to rethink your schema. Have fun :-)

        Project coordinator of the Generic Model Organism Database Project

        One more reason to use PostgreSQL.
Re: Escaping parentheses in regexps
by Anonymous Monk on Jan 23, 2003 at 04:36 UTC
    if ($concat !~ /\Q$string\E/) { $concat .= " and $string"; } # do not interpret $string as regex ... man perlre

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://229024]
Approved by scain
Front-paged by scain
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2016-12-03 07:16 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (52 votes). Check out past polls.