Find all the patterns of \"10+1\" in a given string using Python Regex

The term "10+1" is a specific pattern in a binary string that starts with a digit '1' followed by at least one or more '0' and ending with a '1'. In regular expressions, this pattern is represented as 10+1.

Understanding the Pattern

The regex pattern 10+1 breaks down as follows ?

  • 1 − matches the literal character '1'
  • 0+ − matches one or more occurrences of '0'
  • 1 − matches the literal character '1'

Using re.findall()

The re.findall() method accepts a pattern and a string as parameters, finds the given pattern in the entire string, and returns all the matches in the form of a list.

To find the patterns of "10+1" in a given string, we just need to pass the same as pattern to the findall() method along with the string.

Example

In the following example, we will use the re.findall() function to find all "10+1" patterns in a string ?

import re

text = "10000001 hello world 10011 test100000001test"
pattern = "10+1"
result = re.findall(pattern, text)
print("Found patterns:", result)
print("Number of matches:", len(result))
Found patterns: ['10000001', '1001', '100000001']
Number of matches: 3

Finding Pattern Positions

To find both the patterns and their positions in the string, use re.finditer() ?

import re

text = "10000001 hello world 1001 test100000001test"
pattern = "10+1"

for match in re.finditer(pattern, text):
    print(f"Pattern '{match.group()}' found at position {match.start()}-{match.end()-1}")
Pattern '10000001' found at position 0-7
Pattern '1001' found at position 20-23
Pattern '100000001' found at position 28-36

Real-World Example

Here's a practical example analyzing binary data ?

import re

binary_data = "110010011001000011010000110000111"
pattern = "10+1"
matches = re.findall(pattern, binary_data)

print("Binary string:", binary_data)
print("10+1 patterns found:", matches)
print("Pattern analysis:")
for i, match in enumerate(matches, 1):
    zeros_count = len(match) - 2  # subtract the two '1's
    print(f"  Match {i}: '{match}' contains {zeros_count} zeros")
Binary string: 110010011001000011010000110000111
10+1 patterns found: ['1001', '10000001', '1000001']
Pattern analysis:
  Match 1: '1001' contains 2 zeros
  Match 2: '10000001' contains 6 zeros
  Match 3: '1000001' contains 5 zeros

Conclusion

The regex pattern 10+1 effectively identifies binary sequences starting and ending with '1' containing one or more zeros. Use re.findall() to get all matches or re.finditer() to include position information.

Updated on: 2026-03-24T20:48:43+05:30

284 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements