Yeris Yeris - 3 months ago 18
Python Question

Regular expression matching more than one occurrences of given groupe

I'm struggling with regular expression. I test it out using this
so its not my code (not yet :P). So, i have a long and nasty string, looks more or less like that:


<im a really nasty string from hell/>
<still a nasty string from hell/>
<oh, this is part i need/><
<im a really nasty string from hell/&gt
;

what i want:


<oh, this is part i need/>


i try to catch it with that:

(&#xA\;<)(.|\n|\t)*?(need)(.|\n|\t)*?(\/>)


but it catches to much.. like that:


<im a really nasty string from hell/>
<still a nasty string from hell/>
<oh, this is part i need/>


so end part works as intended, but it grabs to much at the beginning, and I'm not sure why.

Dear internet, plz help :D

Answer Source

You could require that your captured string does not include a slash:


<([^\/]*?need[\S\s]*?)\/>

Or, alternatively, you could allow slashes, but not >. In that case do:


<((?:(?!\/>)[\S\s])*?need[\S\s]*?)\/>