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

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries