[PCRE] Understanding Negative Lookahead for matching specific XML element
Hello. I am trying to understand this PCRE regex: <food>(?:(?!\s*<food>).*\n)+<\/food>\s+<\/menu>\Z from https://regex101.com/r/XHcpeO/1 which matches the last food element (Desert) and the closing menu element tag.
I am aware that the Negative Lookahead (?!) indicates that in order to only match the last food element (Desert), <food> must not be followed by 0 or more spaces (\s*) and should not be followed by another <food> tag.
Why is \s* used in <food>(?:(?!\s*<food>).*\n)+<\/food>\s+<\/menu>\Z and not [\S\s|+?
like: <food>(?:(?![\S\s]+<food>).*\n)+<\/food>\s+<\/menu>\Z?
since in order to not match the food elements for Burgers and Fries, <food> must not be followed by [\S\s]+ and should also not be followed by an opening <food> tag?
Posted Dec 1, 2019, 1:39 PM UTC · edited