Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Using Variables In Perl Regular Expressions (http://www.go4expert.com/articles/using-variables-perl-regular-expressions-t6250/)

pradeep 7Sep2007 22:29

Using Variables In Perl Regular Expressions
I am assuming from now on that you are familiar with substitution operator in perl: s///. A basic example:
Code: Perl

$str =~ s/apple/orange/;

would replace the word "apple" with the word "orange". The separator "/" we used in this example can be replaced with any other non alpha-numeric character. The catch is; you have to escape the separator character inside your regular expression. So it is a better idea to use a less common character as a separator than "/". I prefer using "!" as a separator, because it is less common in strings and visually it is a good separator. So same regular expression could be written as:
Code: Perl

$str =~ s!apple!orange!;

A common mistake people do when using regular expressions is to try to match a variable in your regular expressions.

Code: Perl

$data =~ s!$url!http://go4expert.com!;

This is going to work properly most of the time. But sometime it won't behave as expected or you will be experiencing occasional run time errors. For example, if your $url is equal to http://yahoo.com/do.cgi?action=go++&tell=perl, the substitution operator is going to fail and exit with an error message.


"/http://yahoo.com/do.cgi?action=go++&tell=perl/: nested *?+ in regex..."
The reason for the failure is that you can't use "++" inside your regular expression. You have to escape them. The variable might include several special variables, which have to be escaped properly. To correct way to implement this substitution is:

Code: Perl

$temp = quotemeta($url);
   $data =~ s!$temp!http://yahoo.com!;

quotemeta() is a standard perl function and it escapes all non-alphanumeric characters in your variable.

All times are GMT +5.5. The time now is 21:06.