Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Maybe Escape in Dynamic RegExps

by stefan k (Curate)
on Apr 30, 2004 at 08:14 UTC ( #349355=perlquestion: print w/replies, xml ) Need Help??
stefan k has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,
it has already been hard enough to find a good title for this question and even harder for me to find the solution to this problem (which -I am sure- must be very plain and simple, because I am looking for it so long(*))

I am building a regexp dynamically, say

$re = qr(/($<!$a)$b/);
(using a look behind just to make it a little more spicy) where $a and $b will always contain exactly one char, but that char might be special in regexps. For example $a can be "?" or "-" depending on the current situation.

The question is: is there any way (a module at CPAN which I have overlooked or an operator I didn't fully understand or ...) to dynamically escape those w/o me going through a list of special chars and checking them?

(*) A simple rule of programming: the longer you search (for an error, for a solution, for the correct way, ...) the plainer is the answer (read as: the more stupid was the error, the simpler is the solution, the cleaner is the correct way ...) ;-)

Regards... Stefan
you begin bashing the string with a +42 regexp of confusion

Replies are listed 'Best First'.
Re: Maybe Escape in Dynamic RegExps
by saintmike (Vicar) on Apr 30, 2004 at 08:18 UTC
    Just escape them via the quotemeta() function or, better yet, using \Q and \E:
    $re = qr(/($<!\Q$a\E)\Q$b\E/);
Re: Maybe Escape in Dynamic RegExps
by gellyfish (Monsignor) on Apr 30, 2004 at 08:19 UTC

    The simplest way would be to use the \Q and \E in the expression:

    $re= qr(/($<!\Q$a\E)\Q$b/);
    and it will do the right thing.


Re: Maybe Escape in Dynamic RegExps
by Zaxo (Archbishop) on Apr 30, 2004 at 08:27 UTC

    This does what you want. my $re = qr/(?<!\Q$c\E)\Q$d\E/;
    The \Q . . \E pair is like quotemeta. I've changed the names to protect sort. Extra ( . . ) delimiter pair is cut.

    After Compline,

Re: Maybe Escape in Dynamic RegExps
by halley (Prior) on May 02, 2004 at 14:10 UTC
    The \Q and \E is the correct answer; the quotemeta() function is the equivalent function.

    However, for perl5 anyway, a useful rule of thumb is that alphanumeric characters need escapes to be special, and everything else need escapes to be normal. There are a lot of non-alphanumeric characters that the escaping would be redundant but not harmful (such as \=).

    [ e d @ h a l l e y . c c ]

Re: Maybe Escape in Dynamic RegExps
by stefan k (Curate) on Apr 30, 2004 at 10:49 UTC
    Thanks to all that replied. It works just fine :-) I knew that it had to be that simple and I do feel kinda stupid right now.

    Regards... Stefan
    you begin bashing the string with a +42 regexp of confusion

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://349355]
Approved by Corion
[stevieb]: can anyone recommend a good IDE for C programming that has *proper* vim plugin support? I've tried Eclipse, Netbeans etc and just can't get anything to work properly on Linux. I'm open to paying, but not on some monthly/yearly plan (just a one-off cost)
[stevieb]: I'd go with CLion as it's very similar to what I use already (intelliJ for Perl, Pycharm for Python), but it's a recurring fee every year

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-01-18 19:07 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (163 votes). Check out past polls.