0

mystring = "q1)whatq2)whenq3)where"

want something like ["q1)what", "q2)when", "q3)where"]

My approach is to find the q\d+\) pattern then move till I find this pattern again and stop. But I'm not able to stop.

I did req_list = re.compile("q\d+\)[*]\q\d+\)").split(mystring)

But this gives the whole string. How can I do it?

1 Answer 1

2

You could try the below code which uses re.findall function,

>>> import re
>>> s = "q1)whatq2)whenq3)where"
>>> m = re.findall(r'q\d+\)(?:(?!q\d+).)*', s)
>>> m
['q1)what', 'q2)when', 'q3)where']

Explanation:

  • q\d+\) Matches the string in the format q followed by one or more digits and again followed by ) symbol.
  • (?:(?!q\d+).)* Negative look-ahead which matches any char not of q\d+ zero or more times.
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.