String replace

baybiz's Avatar, Join Date: Feb 2008
Newbie Member
I have a string by "This is apples, it costs me &xyz and I bought them at &abc"

I need to get the folowing output

"This is apples, it costs me {xyz} and I bought them at {abc}"

Basically I need to replace &word with {word}

Thanks
Sri
baybiz's Avatar, Join Date: Feb 2008
Newbie Member
Words beginning with & needs to be replaced with {} around them. Please help
naveen's Avatar
Go4Expert Member
Code:
$string =~ s!&(\S+)!{$1}!g;  ## $string contains the string
murugaperumal's Avatar, Join Date: Feb 2010
Go4Expert Member
Dear friend,

You can use the following way also.

Code:
 
$var="This is apples, it costs me &xyz  and I bought them at &abc";
$var=~s!&([a-z]{3})!{$1}!g;
print "$var\n";
Abinila's Avatar, Join Date: Feb 2010
Light Poster
You can also use the following script.

$str=~s/&(\w+)/{$1}/g
print $str
ungalnanban's Avatar, Join Date: Feb 2010
Go4Expert Member
See the following code. It will match & followed by n number of
numbers/characters.

Code:
$val =~ s/&(.*) /{$1}/g;
print $val;
ungalnanban's Avatar, Join Date: Feb 2010
Go4Expert Member
Quote:
Originally Posted by ungalnanban View Post
See the following code. It will match & followed by n number of
numbers/characters.

Code:
$val =~ s/&(.*) /{$1}/g;
print $val;
In above code(previous post) space will not come, now I correct it.

Code:
$val =~ s/&(.*)/{$1}/g;
print $val;



sorry, I think while copy and pasting time the space is added.
softmarisaravanan's Avatar, Join Date: Jun 2010
Newbie Member
hi friend,

pls try to do it. its working well.

$str =~ s/&(\w+)/{$1}/gs;