1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

String replace

Discussion in 'Perl' started by baybiz, Feb 8, 2008.

  1. baybiz

    baybiz New Member

    Joined:
    Feb 8, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. baybiz

    baybiz New Member

    Joined:
    Feb 8, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Words beginning with & needs to be replaced with {} around them. Please help
     
  3. naveen

    naveen New Member

    Joined:
    Jun 2, 2005
    Messages:
    39
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Calcutta, India
    Home Page:
    Code:
    $string =~ s!&(\S+)!{$1}!g;  ## $string contains the string
     
    shabbir likes this.
  4. murugaperumal

    murugaperumal New Member

    Joined:
    Feb 20, 2010
    Messages:
    15
    Likes Received:
    1
    Trophy Points:
    0
    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";
    
     
  5. Abinila

    Abinila New Member

    Joined:
    Feb 20, 2010
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    You can also use the following script.

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

    ungalnanban New Member

    Joined:
    Feb 19, 2010
    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Chennai
    See the following code. It will match & followed by n number of
    numbers/characters.

    Code:
    [COLOR=Blue]$val =~ s/&(.*) /{$1}/g;
    print $val;[/COLOR]
    
     
  7. ungalnanban

    ungalnanban New Member

    Joined:
    Feb 19, 2010
    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Chennai
    In above code(previous post) space will not come, now I correct it.

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



    sorry, I think while copy and pasting time the space is added.
     
  8. softmarisaravanan

    softmarisaravanan New Member

    Joined:
    Jun 25, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Perl Programmer
    Location:
    chennai
    hi friend,

    pls try to do it. its working well.

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

Share This Page