|Do you know where your variables are?|
Re: regexp w/ special charsby davido (Cardinal)
|on Aug 28, 2003 at 16:56 UTC||Need Help??|
Perl gives you several mechanisms to ensure that you are able to put what might otherwise look like metacharacters into a match string as literal characters rather than metacharacters. In your example above, the cleanest method would be to write the substitution regexp like this:
In the preceeding example, the \Q and \E tell the regexp engine to treat everything between those two tags as literal characters. They can also be used within the text of a regexp, for example:
In the preceeding example, everything between the \Q and \E is treated as literal characters, and everything else is treated as a regular expression (which matches any letter between A and Z repeated three times).
Another way to do it is to employ the quotemeta() function like this:
And then write the substitution regexp like this:
You'll have to decide which of those methods makes the most sense for your particular situation. There is YAWTDI (Yet Another Way To Do It). That is to escape the characters that would be seen as special (or meta-)characters.
And then you would write the substitution regexp like this:
The third method is sometimes clumsy, and sometimes efficient. If, for example, the string contained in $token was intended to actually contain some metacharacters, and at the same time, contain literal characters that would be mistaken as metacharacters, escaping the literals that could be mistaken as metas suddenly becomes a better way to do it.
Hope this helps.
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein