Yeris
Yeris

Reputation: 59

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/>

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.

Upvotes: 0

Views: 37

Answers (1)

trincot
trincot

Reputation: 350272

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]*?)\/>

Upvotes: 1

Related Questions