CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
string match and replace

 



nagsen
New User

Mar 16, 2012, 4:16 PM

Post #1 of 2 (612 views)
string match and replace Can't Post

$source = "John\\Brad";
$target = "Jane";

$sourcename = "John\\Brad Smith";
$targetname = $sourcename;

$targetname =~ s/$source/$target/g;

print"\n sourcename = $sourcename \n";
print"\n targetname = $targetname \n";

------
In the above example, why the value of $targetname is "John\Brad Smith" instead of "Jane Smith" ?


wickedxter
User

Mar 17, 2012, 3:11 PM

Post #2 of 2 (602 views)
Re: [nagsen] string match and replace [In reply to] Can't Post

its bevause the \\ are meta chars used in regex's so i added \Q and \E to escape any meta chars and prints Jane Smith now...



Code
my $source = 'John\\Brad';  
my $target = "Jane";

my $sourcename = 'John\\Brad Smith';
my $targetname = $sourcename;

$targetname =~ s/\Q$source\E/$target/g;

print"\n sourcename = $sourcename \n";
print"\n targetname = $targetname \n";


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives