Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python regular expression repeating pattern matching entire string

Tags:

python

regex

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?

like image 721
feminkk Avatar asked Aug 28 '26 11:08

feminkk


2 Answers

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

like image 174
hannes Avatar answered Aug 29 '26 23:08

hannes


>>> s = "startabcsdendsffstartsdfsdfendsstartdfsfend."
>>> import re
>>> p = re.compile('start(.*?)end')
>>> p.findall(s)
['abcsd', 'sdfsdf', 'dfsf']
like image 45
Patricio Molina Avatar answered Aug 30 '26 01:08

Patricio Molina