Replace numbers after the . with other numbers
-
Is this a CSV file? I would say use File.ReadAllLines and then use Regular Expressions to find the decimal numbers. Then using the Matches collection you can replace digits. You can also use String.Replace method alone to do this.
Thanks... the problem I am facing is that how can I make the code to replace any number (not specify number) with a specify number. for example, I have these numbers in the file: -9999.1234 -9999.2345 -4021.0101 -9999.0987 -9999.7654 -7892.4132 -9999.0393 and etc... I want any number that has -9999. (like -9999.1234, -9999.2345 and etc...) to be replaced with -9999.0000 I want a code that can do that in C#. Note: I wrote a code to read from the file and put each line in a array of type string. What I want is that a code to replace any number that has -9999. (like -9999.1234, -9999.2345 and etc...) with -9999.0000 Regards,
-
Thanks... the problem I am facing is that how can I make the code to replace any number (not specify number) with a specify number. for example, I have these numbers in the file: -9999.1234 -9999.2345 -4021.0101 -9999.0987 -9999.7654 -7892.4132 -9999.0393 and etc... I want any number that has -9999. (like -9999.1234, -9999.2345 and etc...) to be replaced with -9999.0000 I want a code that can do that in C#. Note: I wrote a code to read from the file and put each line in a array of type string. What I want is that a code to replace any number that has -9999. (like -9999.1234, -9999.2345 and etc...) with -9999.0000 Regards,
Quick and dirty way:
string sFile = System.IO.File.ReadAllText(@"c:\\test.txt"); string sRegEx = @"(\\.\[0-9\]\[0-9\]\[0-9\]\[0-9\])"; foreach (Match oMatch in Regex.Matches(sFile, sRegEx)) { sFile = sFile.Replace(oMatch.Value.ToString(), ".0000"); } System.IO.File.WriteAllText(@"c:\\test.txt", sFile);
I would suggest you to search for a better Regular expression since, here I have hardcoded the number of digits after decimal.
-
Thanks... the problem I am facing is that how can I make the code to replace any number (not specify number) with a specify number. for example, I have these numbers in the file: -9999.1234 -9999.2345 -4021.0101 -9999.0987 -9999.7654 -7892.4132 -9999.0393 and etc... I want any number that has -9999. (like -9999.1234, -9999.2345 and etc...) to be replaced with -9999.0000 I want a code that can do that in C#. Note: I wrote a code to read from the file and put each line in a array of type string. What I want is that a code to replace any number that has -9999. (like -9999.1234, -9999.2345 and etc...) with -9999.0000 Regards,
-
Quick and dirty way:
string sFile = System.IO.File.ReadAllText(@"c:\\test.txt"); string sRegEx = @"(\\.\[0-9\]\[0-9\]\[0-9\]\[0-9\])"; foreach (Match oMatch in Regex.Matches(sFile, sRegEx)) { sFile = sFile.Replace(oMatch.Value.ToString(), ".0000"); } System.IO.File.WriteAllText(@"c:\\test.txt", sFile);
I would suggest you to search for a better Regular expression since, here I have hardcoded the number of digits after decimal.
d@nish wrote:
string sRegEx = @"(\.[0-9][0-9][0-9][0-9])";
you can write
string sRegEx = @"(\.[0-9]{4})";
:)
himanshu
-
Thanks... the problem I am facing is that how can I make the code to replace any number (not specify number) with a specify number. for example, I have these numbers in the file: -9999.1234 -9999.2345 -4021.0101 -9999.0987 -9999.7654 -7892.4132 -9999.0393 and etc... I want any number that has -9999. (like -9999.1234, -9999.2345 and etc...) to be replaced with -9999.0000 I want a code that can do that in C#. Note: I wrote a code to read from the file and put each line in a array of type string. What I want is that a code to replace any number that has -9999. (like -9999.1234, -9999.2345 and etc...) with -9999.0000 Regards,
The following regex finds all numbers that conform to the pattern -9.9999, where the first part can have any number of digits (and can be a positive or negative integer), but the part after the period is constrained to 4 digits.
Regex regex = new Regex(
@"(((?<Start>-?)(?<Part>\d*))(\.\d{4}))",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);You can then do a replace on this using the pattern:
${Start}${Part}.0000
This version of the code prevents you from incorrectly grabbing items such as A.1234 or the like. [Edit]Modified because I didn't have Encode HTML tags when pasting set, and the group tags were hidden
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
modified on Tuesday, July 7, 2009 6:44 AM
-
Quick and dirty way:
string sFile = System.IO.File.ReadAllText(@"c:\\test.txt"); string sRegEx = @"(\\.\[0-9\]\[0-9\]\[0-9\]\[0-9\])"; foreach (Match oMatch in Regex.Matches(sFile, sRegEx)) { sFile = sFile.Replace(oMatch.Value.ToString(), ".0000"); } System.IO.File.WriteAllText(@"c:\\test.txt", sFile);
I would suggest you to search for a better Regular expression since, here I have hardcoded the number of digits after decimal.
Or better:
string sFile = System.IO.File.ReadAllText(@"c:\\test.txt"); Regex r = new Regex(@"(\\.\[0-9\]\[0-9\]\[0-9\]\[0-9\])"); sFile = r.Replace(sFile, ".0000"); System.IO.File.WriteAllText(@"c:\\test.txt", sFile);
He still needs to find a better regex though!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
The following regex finds all numbers that conform to the pattern -9.9999, where the first part can have any number of digits (and can be a positive or negative integer), but the part after the period is constrained to 4 digits.
Regex regex = new Regex(
@"(((?<Start>-?)(?<Part>\d*))(\.\d{4}))",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);You can then do a replace on this using the pattern:
${Start}${Part}.0000
This version of the code prevents you from incorrectly grabbing items such as A.1234 or the like. [Edit]Modified because I didn't have Encode HTML tags when pasting set, and the group tags were hidden
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
modified on Tuesday, July 7, 2009 6:44 AM
Pete O'Hanlon wrote:
Regex regex = new Regex( @"(((?-?)(?\d*))(\.\d{4}))", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled );
"parsing "(((?-?)(?\d*))(\.\d{4}))" - Unrecognized grouping construct." This is the exception while using your regex.
-
Pete O'Hanlon wrote:
Regex regex = new Regex( @"(((?-?)(?\d*))(\.\d{4}))", RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled );
"parsing "(((?-?)(?\d*))(\.\d{4}))" - Unrecognized grouping construct." This is the exception while using your regex.
See the modification above. I didn't have "Encode HTML tags when pasting" set, so the group tags were hidden
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
The following regex finds all numbers that conform to the pattern -9.9999, where the first part can have any number of digits (and can be a positive or negative integer), but the part after the period is constrained to 4 digits.
Regex regex = new Regex(
@"(((?<Start>-?)(?<Part>\d*))(\.\d{4}))",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);You can then do a replace on this using the pattern:
${Start}${Part}.0000
This version of the code prevents you from incorrectly grabbing items such as A.1234 or the like. [Edit]Modified because I didn't have Encode HTML tags when pasting set, and the group tags were hidden
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
modified on Tuesday, July 7, 2009 6:44 AM
Pete O'Hanlon wrote:
This version of the code prevents you from incorrectly grabbing items such as A.1234 or the like.
Um, no it doesn't - you need to replace the '*' with a '+' or it matches 0 digits.
Regex regex = new Regex( @"(((?<Start>-?)(?<Part>\d+))(\.\d{4}))",
RegexOptions.IgnoreCase |
RegexOptions.Multiline |
RegexOptions.IgnorePatternWhitespace |
RegexOptions.Compiled );No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Pete O'Hanlon wrote:
This version of the code prevents you from incorrectly grabbing items such as A.1234 or the like.
Um, no it doesn't - you need to replace the '*' with a '+' or it matches 0 digits.
Regex regex = new Regex( @"(((?<Start>-?)(?<Part>\d+))(\.\d{4}))",
RegexOptions.IgnoreCase |
RegexOptions.Multiline |
RegexOptions.IgnorePatternWhitespace |
RegexOptions.Compiled );No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
OriginalGriff wrote:
Um, no it doesn't - you need to replace the '*' with a '+' or it matches 0 digits.
Good spot.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.