Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to keep a regexp from using regexp chars in variables?

This fails: $test='(+boo)'; $match=~/$test/;

         

MichaelBluejay

12:07 pm on Dec 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm having a problem because Perl interprets characters in a variable in a regexp as special regexp characters. For example:

$test = '(+boo)';
$match =~ /$test/;

This fails because Perl interpolates $test and sees the regexp as:

$match =~ /(+boo)/;

It doesn't like that because the + sign is a quantifier, and must follow a "real" character (not an invisible parenthesis).

How do I tell Perl that I want to evaluate a variable in a regexp as a raw string of characters, without trying to act on characters in the variable such as . * + ( )?

phranque

1:02 pm on Dec 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



$test = '(+boo)';

your regular expression contains a metacharacter (+) which you are trying to use as a literal, so you must escape it with the backslash metacharacter thusly:

$test = '(\+boo)';

MichaelBluejay

1:05 pm on Dec 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Perhaps I didn't make myself clear. I understand very well that the problem is that the variable contains a metacharacter. But I'm pulling in the data that I'm processing from logfiles. If I have to do s/// for each and every metacharacter that could exist in each and every line of a huge logfile I'm pulling in then that would affect performance.

Is that really the only way? Seems like a pretty big limitation of Perl of it is.

perl_diver

6:59 pm on Dec 12, 2006 (gmt 0)

10+ Year Member



You probably want to use the \Q....\E options with your regexp. \Q escapes all meta characters in the pattern and \E just tells perl where to end the escaping.

$test = '(+boo)';
$match =~ /\Q$test\E/;

perl_diver

7:05 pm on Dec 12, 2006 (gmt 0)

10+ Year Member



actually I should have said \Q escapes most meta characters, I think it does not esccape $ and @ but would have to check to be sure.

MichaelBluejay

2:19 am on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Whoo-hoo, that's EXACTLY what I'm looking for! I couldn't find it in any of my books. Works great. Less filling. I'm so happy.

phranque

4:05 am on Dec 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



nice tip, p_d!
"i did not know that."

perl_diver

4:54 am on Dec 13, 2006 (gmt 0)

10+ Year Member



It should be covered in any decent regexp tutorial or reference material. It is on the perldoc site although you have to read through quite a bit of stuff to find it.

[perldoc.perl.org...]