Format number number with specific mask regex python
format, mask, python, regex, string-formatting
Solution
You can use this pattern:
(?:(?<=^\d)|(?<=^\d{2})|(?<=^\d{3})|(?<=^\d{4})|(?<=^\d{6}))(?=\d)
with `.` as replacement.
example:
re.sub(r'(?:(?<=^\d)|(?<=^\d{2})|(?<=^\d{3})|(?<=^\d{4})|(?<=^\d{6}))(?=\d)', '.', yourstr)
Problem
I need to format a number with a specifc mask: 9.9.9.9.99.999, depending on the length of number string. For example: ``` - 123456789 => 1.2.3.4.56.789 - 123456 => 1.2.3.4.56 - 1234 => 1.2.3.4 - 123 => 1.2.3 - 12 => 1.2 ``` It will not occur a number string with 7 or 8 digits in the input. How could that be implemented with regex, preferably in python? Thanks in advance.