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:
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:
A common mistake people do when using regular expressions is to try to match a variable in your regular expressions.
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.
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 16:03.|