Reputation: 505
I'm new to python and I'm trying to construct a list of tuples with the start and end indices for pattern matching in a string.
I need to match a pattern that starts with 2 consecutive 0s and ends with 2 consecutive 1s with some combo of 0s and 1s in between.
For example,
s = '00101010111111100001011'
With some type of operation returning,
[(0, 10), (15, 23)]
I can find multiple occurrences of a pattern in a string using,
ind = [(m.start(), m.end()) for m in re.finditer(pattern, s)]
I'm just not sure how to write the regular expression (i.e pattern) to output what I want.
Upvotes: 3
Views: 1408
Reputation: 626689
Use the following pattern:
00[01]*?11
See the regex demo
Details:
00
- two consecutive 0
s[01]*?
- zero or more 0
or 1
chars, as few as possible (as *?
is a lazy quantifier)11
- two consecutive 1
chars.import re
s = '00101010111111100001011'
rx = r'00[01]*?11'
print([(x.start(),x.end()) for x in re.finditer(rx, s)])
# => [(0, 10), (15, 23)]
Upvotes: 6