467,882 Members | 1,212 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,882 developers. It's quick & easy.

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
Aug 5 '08 #1
  • viewed: 1035
Share:
9 Replies
numberwhun
Expert Mod 2GB
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
Aug 5 '08 #2
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.
Aug 5 '08 #3
KevinADC
Expert 2GB
Do not forget to post your code (in code tags)
oops...... hehehe
Aug 5 '08 #4
eWish
Expert 512MB
oops...... hehehe
Maybe numberwhun won't notice.
Aug 6 '08 #5
KevinADC
Expert 2GB
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;
Aug 6 '08 #6
Thanks Kevin for the help. It replaces the negative and decimal numbers.
Aug 6 '08 #7
KevinADC
Expert 2GB
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.
Aug 6 '08 #8
numberwhun
Expert Mod 2GB
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?
Aug 6 '08 #9
KevinADC
Expert 2GB
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.
Aug 6 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by C# Learner | last post: by
7 posts views Thread by VMI | last post: by
12 posts views Thread by Adam J. Schaff | last post: by
2 posts views Thread by Alain | last post: by
32 posts views Thread by FireHead | last post: by
reply views Thread by Lakhi | last post: by
7 posts views Thread by DarthBob88 | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.