Find all the patterns of “1(0+)1” in a given string using Python Regex

A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0’s. Count all such patterns. The patterns are allowed to overlap.

Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern.

Examples:



Input : 1101001
Output : 2

Input : 100001abc101
Output : 2

We have existing solution for this problem please refer Find all the patterns of “1(0+)1” in a given string link. Another set containing similar solution using regex in java is also published.

We will solve this problem quickly in python using Regex. Approach is very simple :

  1. Search a first sub-string in original string which follows ’10+1′ pattern using re.search(regex,string) method.
  2. substr = re.search(regex,string) return None if it doesn’t find given regex as sub-string in original string otherwise it returns first matched sub-string which follows ’10+1′ pattern. substr.start() gives us starting index of matched regex and substr.end() gives us ending index of matched regex.
  3. Whenever we find regex as sub-string then increase count by 1 and again search for given regex starting from ending index of previous sub-string.
filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to Find all the patterns 
# of “1(0+)1” in a given string using Python Regex
  
import re
  
# Function to Find all the patterns
# of “1(0+)1” in a given string
def extract(input):
  
    # search regex '10+1' in original string
    # search() function return first occurrence 
    # of regex '10+1' otherwise None
    # '10+1' means sub-string starting and ending with 1
    # and atleast 1 or more zeros in between
    count=0
    substr = re.search('10+1',input)
      
    # search for regex in original string 
    # untill we are done with complete string
    while substr!=None:
        # if we find any occurrence then increase count by 1
        count=count+1
          
        # find next occurrence just after previous 
        # sub-string
        # for first occurrence 101, substr.start()=1
        # substr.end()=4
        input = input[(substr.end()-1):]
        substr = re.search('10+1',input)
    print (count)
  
# Driver program
if __name__ == "__main__":
    input = '1101001'
    extract(input)

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry Skilled in Big Data technologies, Data Structures, Algorithm, Software Development, App Development, Databases, Python, Java, C, and WordPress

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.