VingInMedina has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to escape a charater in a string. For example, if I have a string that looks like this:
I want to change it to this:This is my string that has a | in it.
This is my string that has a \| in it.
Now, in this specific case I can do it with this simple statement:
My problem is that the character that needs to be escaped is actually held in a variable ($delim). I tried this:$_ =~ s/\|/\\\|/g;
But it doesn't work.$_ =~ s/\{$delim}/\\{$delim}/g;
I know that I could go character by character through the sring, but that seems kinda ugly.
Any ideas would be really helpful.
thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to 'escape' a character in a string?
by moritz (Cardinal) on May 23, 2012 at 16:35 UTC | |
|
Re: How to 'escape' a character in a string?
by toolic (Bishop) on May 23, 2012 at 16:40 UTC | |
|
Re: How to 'escape' a character in a string?
by ikegami (Patriarch) on May 23, 2012 at 17:33 UTC | |
by VingInMedina (Acolyte) on May 23, 2012 at 17:38 UTC |