Terminat
Terminat

Reputation: 1237

Regex to match 3 zeroes in the string of digits

Let's say I have strings like this one, and I need to get rid of three 0 in it. I need to get rid of the last three ones. The numbers that should be created after removing these zeroes can consist only of 2 or 3 digits.

Probably the best idea would be to somehow tell regex to only match when the pattern of 000 is followed by digits from [1-9] and exclude [1-9] from it. However, I have no idea how to do that.

76000123000100000101000 ---> 76 123 100 101

Is it possible to do with regex? I tried many different patterns, but I can't find the right one.

Upvotes: 2

Views: 591

Answers (2)

The fourth bird
The fourth bird

Reputation: 163477

Other options getting the last 3 zeroes are using a negative lookahead (?! asserting not a zero at the right.

000(?!0)

Regex demo | Python demo

import re
 
print(re.sub(r'000(?!0)', ' ', '76000123000100000101000').strip())

Or using a capture group, matching 3 zeroes and capture what follows using that in the replacement:

000([1-9]|$)

Regex demo | Python demo

import re
 
print(re.sub(r'000([1-9]|$)', r'\1 ', '76000123000100000101000').strip())

Output

76 123 100 101

Upvotes: 1

Ryszard Czech
Ryszard Czech

Reputation: 18631

Use

re.sub(r'000(?=[1-9]|$)', ' ', x).strip()

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  000                      '000'
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [1-9]                    any character of: '1' to '9'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead

Python code:

import re
regex = r"000(?=[1-9]|$)"
test_str = "76000123000100000101000"
result = re.sub(regex, " ", test_str).strip()
print(result)

Results: 76 123 100 101

Upvotes: 3

Related Questions