Search code examples
regexbbedit

BBEdit chomp off excessive decimals


Having a very large data set of float values where the precision is not longer needed, what is a regular expression that I can use with BBEdit to allow me to keep a maximum of 5 digits after a period?

Physically, the decimal value always has a character preceeding the period, is always preceeded by a space, but can have a comma or a space after the string.

sample:

 -162.40904700399989, -82.896416924999954 

Solution

  • You may use

    Find:       (\d\.\d{5})\d+
    Replace: \1

    Details

    • (\d\.\d{5}) - Group 1 (referred to via \1 from the replacement pattern): a digit, . and then 5 digits (note the first \d has no quantifier, we are not interested if there are more than one, one is enough, before the decimal separator)
    • \d+ - one or more digits. Note the + quantifier makes more sense than * because we only want to match those numbers that we want to modify, those that already have 5 digits after the decimal separator do not have to be matched.