Search code examples
regextextpad

Regex: rounding all the real number in a text file (keeping the 15 decimal digits)


I have a text file with thousands of entries (the nodes of a mesh) like

7.40000000000060391E+01   7.40000866410523770E+00    
1.05000000970718801E+01   6.40000007900613273E+01
2.40500000000000321E+02   2.40000000428227065E+00   
6.00000000000000000E+00   3.70000085530326430E+01   
7.40000019596598406E+01   6.40000000000000000E+01
3.10000144967919340E+01   1.92000112854581857E+01
6.40000000000000000E+01   6.40004500000000000E+01

where some of my entries have a small error that I would like to remove. I am using textpad. I would like to keep the first numbers until I found a triple zero and set all the other decimal digits to zero. The example above would read:

7.40000000000000000E+01   7.40000000000000000E+00    
1.05000000000000000E+01   6.40000000000000000E+01
2.40500000000000000E+02   2.40000000000000000E+00   
6.00000000000000000E+00   3.70000000000000000E+01   
7.40000000000000000E+01   6.40000000000000000E+01
3.10000000000000000E+01   1.92000000000000000E+01
6.40000000000000000E+01   6.40000000000000000E+01

Any suggestion? Thanks Alberto


Solution

  • If you don't have JGSoft or a .NET engine near to you, you can try regexhero.net. It's an online regular expression tester powered by .NET !

    I have build a demo based on the excellent Tim's proposition.

    You can:

    • quickly cut and paste your entries
    • have them modified by regexhero
    • and then cut-and-paste the result back to your editor

    NOTA: I am not affiliated in any way with regexhero :)