How can I use a regex to replace a varying number of zeroes with the same number of spaces?
-
09-12-2019 - |
Question
I'm working with fixed-length field data, and I need to shorten the decimal places of a series of numbers to only two decimal places. It is nothing but zeroes after the decimal, so it's easy enough to write a regex that will trigger on it. The problem is, I need to replace the extra zeroes with spaces. I know I can save a subexpression and recall it in the replacement, but is there a way to count the number of zeroes removed and recall that? I'm just using the regex in Notepad++'s replace window.
Solution
I've found something that works here.
search string: ([.]00[ ]*)0
replace string: \1
(There's a space following the 1)
What this does is find the .00, followed by any number of spaces, followed by a zero. It then replaces that with the .00 and the spaces, followed by a space. Each time it is run, it replaces one more of the extra zeroes with a space. It works, but can anyone think of something that only has to be run once? This worked in this case, but I've had other times when I wished regex could count.
OTHER TIPS
This worked in this case, but I've had other times when I wished regex could count.
Haven't we all wished that at one time or another :)
The only way I've found to do what you need is to use Python's regular expression library, which allows you to specify a function to do the replace, instead of a replace string. So you could make this call on every line in your file:
re.sub(search_string, replace_func, line)
This is how you would use it:
import re
search_string = r'(\.0{2})(0+)'
# this function is called for each match, and returns the replace string
def replace_func(match):
num_zeroes = len(match.group(2))
if num_zeroes:
return match.group(1) + num_zeroes * ' '
else:
return ''
for line in ('1234.0', '1234.00', '1234.000', '1234.0000'):
print re.sub(search_string, zero_replacer, line)
Unfortunately, this doesn't help you for Notepad++, but if you're interested, I could write up a more complete python script that would read your file and do the work.
you need to run this in a loop to use the back references correctly:
var data = "7.00000000000 5.00000000 8.0 9.000000000000000000000";
var re = /(\.0{0,2})(0*)/;
alert(data.replace(re, RegExp.$1 +RegExp.$2.replace(/0/, 'x')));
edit: first answer collapsed, which is not what the OP wanted.