473,388 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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
9 1138
numberwhun
3,509 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
4,059 Expert 2GB
Do not forget to post your code (in code tags)
oops...... hehehe
Aug 5 '08 #4
eWish
971 Expert 512MB
oops...... hehehe
Maybe numberwhun won't notice.
Aug 6 '08 #5
KevinADC
4,059 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
4,059 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
3,509 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
4,059 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

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

Similar topics

4
by: C# Learner | last post by:
What's the "standard" way of replacing a character in a string? Obviously, I can't say "myString = character;" because strings are immutable... So what would be the "standard" way of doing this?...
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
7
by: Ryan Taylor | last post by:
Hi. I have some code that dynamically generates a PDF and spits this content directly to the web browser. I use HTMLDoc to create the Pdf's from html. So the user can click on a button "Print...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
1
by: WT | last post by:
Hello, I was using hastable to store collection of objects of the same class MyClass. I start replacing this hastable by a Dictionary<string,MyClass>....but I was storing this hastable in cache...
2
by: Alain | last post by:
Hi, I am working on a project where I need to convert international characters with acii values. Like André -> andre and Björn -> bjorn. How can I do this without replacing every single...
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
0
by: Lakhi | last post by:
hi frnds, I need small help in String replacement I need to replace the text using replceAll() with Case-Insensitve . Is there any regular expression for this? i have this expression ...
2
by: gsuns82 | last post by:
Hi all, I have to replace accented characters from a input string with normal plain text.I have coded as follows. String input = "ÄÀÁÂÃ"; input=...
7
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.