mwhiting has asked for the wisdom of the Perl Monks concerning the following question:
... at least I think it will be.
I need to search a string for characters that will be interpreted as metacharacters, like + $ ^ etc, and put the backslash in front so they don't get interpreted that way. Here's how I could do it by substituting just one metacharacter at a time:
How can I do it in one line? I'm looking for a metacharacter to put into the second half of the substitution regex that basically says, "Take whatever character you matched from the first part and put it back in here". It might look like:$_ =~ s/\+/\\+/; $_ =~ s/\$/\\$/; $_ =~ s/\^/\\^/;
but with something in place of the *$_ =~ s/(\+|\$|\^)/\\*/;
Thanks in advance - hope the answer's not too obvious & I'm really overlooking something.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple search question ...
by throop (Chaplain) on Jun 13, 2008 at 23:20 UTC | |
|
Re: Simple search question ...
by swampyankee (Parson) on Jun 14, 2008 at 00:47 UTC | |
|
Re: Simple search question ...
by casiano (Pilgrim) on Jun 14, 2008 at 07:39 UTC | |
by mwhiting (Beadle) on Jun 14, 2008 at 17:23 UTC |