String replace

Newbie Member
9Feb2008,04:36   #1
baybiz's Avatar
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
Newbie Member
9Feb2008,06:56   #2
baybiz's Avatar
Words beginning with & needs to be replaced with {} around them. Please help
Go4Expert Member
9Feb2008,13:01   #3
naveen's Avatar
Code:
$string =~ s!&(\S+)!{$1}!g;  ## $string contains the string
Go4Expert Member
25Feb2010,15:04   #4
murugaperumal's Avatar
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";
Light Poster
26Feb2010,13:59   #5
Abinila's Avatar
You can also use the following script.

$str=~s/&(\w+)/{$1}/g
print $str
Go4Expert Member
2Mar2010,10:49   #6
ungalnanban's Avatar
See the following code. It will match & followed by n number of
numbers/characters.

Code:
$val =~ s/&(.*) /{$1}/g;
print $val;
Go4Expert Member
9Mar2010,12:22   #7
ungalnanban's Avatar
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.
Newbie Member
25Jun2010,21:06   #8
softmarisaravanan's Avatar
hi friend,

pls try to do it. its working well.

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