Regex needed to replace "0" either preceded, followed or surrounded by "1" in string of zeros and ones

Question:

Given the string "001000100", I want to replace only the zeros surrounded by "1" by "1". The result in this case would be "001111100" In this case there’s guaranteed to be only one sequence of zeros surrounded by ones.

Given the string "100" or "001" or "110" or "011", I want the original string returned.

Performance is not an issue as the string (which is currently "101"), is only expected to increase slowly over time when electricity and/or tax rates change.

I think this should be trivial but my limited regex experience and web searches have failed to come up with an answer. Any help coming up with the relevant regex pattern will be appreciated.

EDIT: since posting this question, I’ve received quite a bit of useful feedback. To ensure any answers address my requirements I’ve rethought the requirements and I think (since I’m still not 100% certain) that they can be summarized as follows:

  1. ‘string’ shall always contain at least one 1
  2. ‘string’ shall have zero or one sequence of one or more 0 surrounded by a 1
  3. a sequence of one or more 0 surrounded by a 1 shall be replaced by the same number of 1
  4. ‘string’ that does not have at least one 0 surrounded by 1 shall be returned as-is

Another useful piece of information is that the original input is not a string but a Python list of Booleans. Therefore any solution that uses regex will have to convert the list of Booleans to a string and vice versa.

Asked By: Alfred Elkerbout

||

Answers:

I solved my problem thanks to the essential contributions of Kelly Bundy and bobble bubble. The following Python function meets the requirements but improvements are of course welcome:

    def make_contiguous(booleans):  # replaces '0' surrounded by '1' into '1'
        string = "".join(str(int(i)) for i in booleans)  # convert list of Booleans to str to allow use of regex
        string = re.sub('10*1', lambda m: '1' * len(m[0]), string)  # apply the regex
        string = list(string)  
        booleans = [int(i) for i in string]  # convert the str back to Booleans
        return booleans
Answered By: Alfred Elkerbout
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.