I'm trying to resolve the following scenario with javascript regexp:
I have the following text:
<foo>a</foo>blah <bar>blah</bar><abc>dsdfsdf</abc> blah<foo>b</foo><blah></blah>{COMPANY_NAME}
I would like to catch the {COMPANY_NAME}
with text inside the previous foo tag.
so here i'm trying to find the text b
that exists inside the first foo tag behind the COMPANY_NAME.
the only way i found to do that is to start with a greedy character, which means to use this:
/.*<foo>(.*?)<\/foo>.*?{COMPANY_NAME}/
if I don't start with the greedy selection at the begginging (.*
) then it will provide the first foo instead of the last one, the problem is that this document is really big and it takes a lot of memory and it takes a long time to return each match and i'mm doing a lot of matches.
is there a way to resolve it without starting with the greedy .*
? and just to return the last foo properly so the match will only contain what i need and not more then that ?
thanks
CodePudding user response:
You may use this regex without initial greedy .*
:
<foo>((?:(?!<\/?foo>).)*?)<\/foo>(?:(?!<\/?foo>).)*?{COMPANY_NAME}
Here (?:(?!<\/?foo>).)*?
is tempered greedy pattern that matches one or more characters where each character must not be followed by <foo>
or </foo>
.