Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   String replace (http://www.go4expert.com/forums/string-replace-t8724/)

baybiz 9Feb2008 04:36

String replace
 
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 9Feb2008 06:56

Re: String replace
 
Words beginning with & needs to be replaced with {} around them. Please help

naveen 9Feb2008 13:01

Re: String replace
 
Code:

$string =~ s!&(\S+)!{$1}!g;  ## $string contains the string

murugaperumal 25Feb2010 15:04

Re: String replace
 
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 26Feb2010 13:59

Re: String replace
 
You can also use the following script.

$str=~s/&(\w+)/{$1}/g
print $str

ungalnanban 2Mar2010 10:49

Re: String replace
 
See the following code. It will match & followed by n number of
numbers/characters.

Code:

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


ungalnanban 9Mar2010 12:22

Re: String replace
 
Quote:

Originally Posted by ungalnanban (Post 64986)
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 25Jun2010 21:06

Re: String replace
 
hi friend,

pls try to do it. its working well.

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


All times are GMT +5.5. The time now is 22:31.