|
I intend to write a perl code that goes someting like this:
$mytest1 = "\abc";
$myrep1 = "@\abc";
$mystring =~ s/$mytest1/$myrep1/g;
In other words, I need this to replace every occurence of \abc with @\abc.
I tried using $mytest1 = "\\abc";
but that does not seem to work..in fact it replaces even instances of "abc" with "@abc".
Any suggestions on how to do this one?
thx
Anand
|
|
|
to replace every occurence of \abc with @\abc.
i think the following should work:
$mystring =~ s/\\abc/\@\\abc/gi;
|
|
|
lots of ppl have problems with perl regex but once you learn the basics it becomes fun to use and can save lots of time.
my example has only been tested with the test sting below, it should be able to handle all cases.
my $mystring = '555\abc\abcTTT\abc4421';
my $mytest1 = '\abc'; # Note (1) The way I quoted
my $myrep1 = '@\abc';
$mystring =~ s/\Q$mytest1\E/$myrep1/g; # Note 2 \Q and \E
print $mystring;
Note (1): The quote used for the 2 test/replace strings will disable any types of regex and display the text as it is.
Note (2): \Q is for "quote (disable) pattern metacharacters till \E" and \E "end case modification".
Reference Guide: http://perldoc.perl.org/perlre.html#Regular-Expressions http://perldoc.perl.org/perlreref.html
|
|
|
|
|
|
|
// |