Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 5th, 2008, 09:16 PM
Member
 
Join Date: Jul 2008
Posts: 37
Default Replacing the string

Hello All,

I would like the replace the following string,

TRANS -847.0 -672.0 -71.0

as

TRANS -840.0 -660.0 -62.0

I am able to replace the string when the numbers are positive without decimals using \d and \s syntax. But the above expressions have negative sign and decimals. How could i formulate that using regexp.

Ramesh
Reply
  #2  
Old August 5th, 2008, 10:41 PM
numberwhun's Avatar
Forum Leader
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,156
Default

What have you tried thus far? I would love to see what you come up with and will then help you from there. Do not forget to post your code (in code tags) and any errors or results.

Regards,

Jeff
Reply
  #3  
Old August 5th, 2008, 11:16 PM
Member
 
Join Date: Jul 2008
Posts: 37
Default

Expand|Select|Wrap|Line Numbers
  1. #!\usr\bin\perl
  2. use warnings;
  3. use strict;
  4. open IN,"+< z.txt";  
  5. open OUT,"> t.txt";  
  6. while (<IN>) {
  7. s/(\d+)\s+(\d+)\s+(\d+)/ ($1+7).$2. ($3+12).$2.($4+9)/g;
  8. print OUT;
  9. }
This works when numbers are positive integers. My case is for decimals and negative numbers.

Last edited by eWish; August 6th, 2008 at 02:01 AM. Reason: Added code tags
Reply
  #4  
Old August 5th, 2008, 11:51 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Posts: 3,662
Default

Quote:
Do not forget to post your code (in code tags)
oops...... hehehe
Reply
  #5  
Old August 6th, 2008, 02:01 AM
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 837
Default

Quote:
Originally Posted by KevinADC
oops...... hehehe
Maybe numberwhun won't notice.
Reply
  #6  
Old August 6th, 2008, 05:23 AM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Posts: 3,662
Default

Expand|Select|Wrap|Line Numbers
  1. my $str = 'TRANS -847.0 -672.0 -71.0';
  2. $str =~ s/(\S+)\s+(-?\d+\.?\d?)\s+(-?\d+\.?\d?)\s+(-?\d+\.?\d?)/sprintf "$1 %.1f %.1f %.1f", $2+7,$3+12,$4+9/eg;
  3. print $str;
Reply
  #7  
Old August 6th, 2008, 06:51 AM
Member
 
Join Date: Jul 2008
Posts: 37
Default

Thanks Kevin for the help. It replaces the negative and decimal numbers.
Reply
  #8  
Old August 6th, 2008, 06:59 AM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Posts: 3,662
Default

Quote:
Originally Posted by ramesh54
Thanks Kevin for the help. It replaces the negative and decimal numbers.
Next time use the CODE tags, OK? If not, Jeff might ban you from the forum.
Reply
  #9  
Old August 6th, 2008, 12:56 PM
numberwhun's Avatar
Forum Leader
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,156
Default

Quote:
Originally Posted by KevinADC
Next time use the CODE tags, OK? If not, Jeff might ban you from the forum.
I am not that cruel, I give a couple warnings. :-) Don't you have a rebellion to worry about Kevin?
Reply
  #10  
Old August 6th, 2008, 06:19 PM
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Posts: 3,662
Default

Quote:
Originally Posted by numberwhun
I am not that cruel, I give a couple warnings. :-) Don't you have a rebellion to worry about Kevin?

No, the rebellion was postponed, maybe cancelled, depends on if my wife comes to her senses.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles