Search code examples
regexcompressionnotepad++geojson

RegEx to reduce the decimal places to 5 digits in notepad++


I am trying to reduce the size of a geoJSON file so my website viewers can view the maps in page very quickly.

You can find more information about geoJSON format here http://geojson.org/

I read a blog suggesting to reduce the number of digits after decimal places in a GeoJSON file using notepad ++.

I can find answers for removing all decimal places in a number. But my question is I want to preserve the first 5 decimal places in a number and remove the others.

EG: -103.3751447563353

After replacing: -103.37514

Edit: I tried the answers but my notepad++ says "can't find the text". I have ensured regular expression checkbox is checked but still no luck This will save more than 10 characters for each latitude or longitude co-ordinates.

Please share your answers


Solution

  • See regex in use here

    (?<=\d\.\d{5})\d+
    
    • (?<=\d\.\d{5}) Positive lookbehind ensuring what precedes is a digit, dot, and then 5 digits
    • \d+ Matches one or more digits (this is what will be replaced)

    Replace with nothing


    Another alternative. See regex in use here

    \d+\.\d{5}\K\d+
    
    • \d+ Match one or more digits
    • \. Match the dot character literally
    • \d{5} Match any digit exactly 5 times
    • \K Resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
    • \d+ Matches one or more digits (this is what will be replaced)

    Replace with nothing