2

I am working with python regular expression basics .I have a string

startabcsdendsffstartsdfsdfendsstartdfsfend.

How do i get the strings in between consecutive start and end without matching the entire string?

0

2 Answers 2

6

use the start.*?end in the re. The question mark means "as few as possible".

Sign up to request clarification or add additional context in comments.

3 Comments

Almost - just needs a capture group adding to exclude the terminators from the result group start(.*?)end
This answer should be correct. For further googling needs, you should search of "greedy" vs "non-greedy" regular expressions.
exactly what I was looking for
4
>>> s = "startabcsdendsffstartsdfsdfendsstartdfsfend."
>>> import re
>>> p = re.compile('start(.*?)end')
>>> p.findall(s)
['abcsd', 'sdfsdf', 'dfsf']

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.