Backreference and Substitution problems

K

Krishna Srinivasan

$foo = "This is a BlueTooth device";
$bar = "bluetooth";

print $foo . "\n";

$foo =~ s/$bar/<$1>/ig;
print $foo . "\n";

The output I expect is "This is a <BlueTooth> device"
But I get "This is a <> device"

Where am I going wrong?

Krishna.
 
A

Anno Siegel

Krishna Srinivasan said:
$foo = "This is a BlueTooth device";
$bar = "bluetooth";

print $foo . "\n";

$foo =~ s/$bar/<$1>/ig;
print $foo . "\n";

The output I expect is "This is a <BlueTooth> device"
But I get "This is a <> device"

Where am I going wrong?

You're not capturing the search result, so $1 is undefined.

$foo =~ s/($bar)/<$1>/ig;

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top