3

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.

0

1 Answer 1

6

Use the following pattern:

00[01]*?11

See the regex demo

Details:

  • 00 - two consecutive 0s
  • [01]*? - zero or more 0 or 1 chars, as few as possible (as *? is a lazy quantifier)
  • 11 - two consecutive 1 chars.

Python demo:

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)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.