0

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.

4
  • Put this (.|\n|\t)*?(need)(.|\n|\t)*? in another Subpattern ((.|\n|\t)*?(need)(.|\n|\t)*?). Commented Jul 2, 2017 at 17:36
  • Nop, this still catches to much. I also tried ((&#xA\;<)(.|\n|\t)*?).(need)(.|\n|\t)*?(\/>) but its still grabs to much. Commented Jul 2, 2017 at 17:45
  • Do you know what characters should be matched or should not be matched? If yes, then it is easy. Commented Jul 2, 2017 at 17:55
  • Not characters, groups of those yes, but not single char :) Commented Jul 2, 2017 at 18:06

1 Answer 1

1

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

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

1 Comment

thank you, that second solution did the trick, and i can use it in all use-cases that i have.

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.