[Solved] RegEx matching for removing a sequence of variable length in a number


I assume X and Y can’t begin with 0. [1-9]\d{0,2} matches a number from 1 to 3 digits that doesn’t begin with 0.

So the regexp to extract X and Y should be:

^([1-9]\d{0,2})000([1-9]\d{0,2})000$

Then you can use re.sub() to remove the zeroes between X and Y.

regex = re.compile(r'^([1-9]\d{0,2})000([1-9]\d{0,2})000$');
i = 14000010000
istr = str(i)
new_i = int(regex.sub(r'\1\2', istr)

You can map this over your numpy array

regex = re.compile(r'^([1-9]\d{0,2})000([1-9]\d{0,2})000$');
new_array = np.fromiter((int(regex.sub(r'\1\2', str(x)) for x in array), array.dtype)

See Most efficient way to map function over numpy array for various ways of mapping a function over a numpy array.

2

solved RegEx matching for removing a sequence of variable length in a number