Change language

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

| | | | |

Note. Contains numbers and lowercase letters only. The string is not necessarily binary. 100201 is not a valid template.

Examples:

 Input: 1101001 Output: 2 Input: 100001abc101 Output: 2 

We have a solution to this problems, please refer to link. Also posted another set containing a similar solution Regex . The approach is very simple:

  1. Find the first substring in the original string that matches the pattern "10 +1" using method re.search (regex, string) .
  2. substr = re.search (regex, string) returns None, if it does not find the given regexp as a substring in the original string, otherwise it returns the first matched substring that follows the pattern "10 +1".  substr.start () gives us the starting index of the matching regular expression, and substr.end () gives us the ending index of the matching regular expression.
  3. Whenever when we find a regular expression as a substring, increment the counter by 1 and look for the given regular expression again, starting at the ending index of the previous substring.

# Python program to find all templates
# "1 (0+) 1" on a given string using Python Regex

 

import re

 
# Function Find all templates
# from "1 (0+) 1" on this line

def extract ( input ):

  

  # search regex & # 39; 10 +1 & # 39; in original line

The # search () function returns the first occurrence

# regular expression & # 39; 10 +1 & # 39; otherwise no

# & # 39; 10 +1 & # 39; means a substring that starts and ends with 1

# and at least 1 or more zeros between

count = 0

substr = re.search ( ’10 + 1’ , input )

  

# search for regex in source string

# until we’re done with the full line

while substr! = None :

# if we find any incident, increase the number by 1

count = count + 1

 

  # find the next occurrence immediately after the previous one

# substring

# 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 )

Output:

 2 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method