 | 
August 5th, 2008, 09:16 PM
| | Member | | Join Date: Jul 2008
Posts: 37
| | 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
| 
August 5th, 2008, 10:41 PM
|  | Forum Leader | | Join Date: May 2007 Location: New Hampshire
Posts: 2,156
| |
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
| 
August 5th, 2008, 11:16 PM
| | Member | | Join Date: Jul 2008
Posts: 37
| | - #!\usr\bin\perl
-
use warnings;
-
use strict;
-
open IN,"+< z.txt";
-
open OUT,"> t.txt";
-
while (<IN>) {
-
s/(\d+)\s+(\d+)\s+(\d+)/ ($1+7).$2. ($3+12).$2.($4+9)/g;
-
print OUT;
-
}
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
| 
August 5th, 2008, 11:51 PM
|  | Expert | | Join Date: Jan 2007
Posts: 3,662
| | Quote: |
Do not forget to post your code (in code tags)
| oops...... hehehe
| 
August 6th, 2008, 02:01 AM
|  | Moderator | | Join Date: Jul 2007 Location: Arkansas
Posts: 837
| | Quote: |
Originally Posted by KevinADC oops...... hehehe | Maybe numberwhun won't notice.
| 
August 6th, 2008, 05:23 AM
|  | Expert | | Join Date: Jan 2007
Posts: 3,662
| | - my $str = 'TRANS -847.0 -672.0 -71.0';
-
$str =~ s/(\S+)\s+(-?\d+\.?\d?)\s+(-?\d+\.?\d?)\s+(-?\d+\.?\d?)/sprintf "$1 %.1f %.1f %.1f", $2+7,$3+12,$4+9/eg;
-
print $str;
| 
August 6th, 2008, 06:51 AM
| | Member | | Join Date: Jul 2008
Posts: 37
| |
Thanks Kevin for the help. It replaces the negative and decimal numbers.
| 
August 6th, 2008, 06:59 AM
|  | Expert | | Join Date: Jan 2007
Posts: 3,662
| | 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.
| 
August 6th, 2008, 12:56 PM
|  | Forum Leader | | Join Date: May 2007 Location: New Hampshire
Posts: 2,156
| | 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?
| 
August 6th, 2008, 06:19 PM
|  | Expert | | Join Date: Jan 2007
Posts: 3,662
| | 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.
|  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | 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.
|