Home > other >  Regex needed to replace "0" either preceded, followed or surrounded by "1" in st
Regex needed to replace "0" either preceded, followed or surrounded by "1" in st

Time:07-22

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.

CodePudding user response:

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
  • Related