"Reverse" order regex - the closest "above" match
this is an example of some string.
<div>other text</div> some text abc , <div>need_match_this</div> bbbb <p>hsa</p> aa <span>hello</span> I only know the end of the line "<span>hello</span>" , and I need to match the text in the nearest "above" div.
I used this regex:
\<div\>(.*?)\<\/div\>.*?\<span\>hello\<\/span\> But this does not work for me, because I need to return only the text of the nearest div, and not the div in the line first.
Is there any regular expression solution to solve this problem?
Please, help.
thanks
Do you need to use a negative expression for regular expressions instead .*? , since .*? will also match opening or closing div tags.
<div>((?:(?!<\/?div>).)*?)<\/div>(?:(?!<\/?div>).)*?<span>hello<\/span> (?:(?!<\/?div>).)*? forces the regex engine to match any character, but not <div> or </div> . That is, before matching each character, this regular expression will check that this particular character is not a start character in a <div> or </div> . If so, then it will match that particular character. If not, the match will abruptly end and the next character will not be matched.
Example:
string - <div></div>
regex - <div>((?:(?!<\/?div>).)*?)<\/div>
For this input, the aforementioned regular expression will capture an intermediate empty string (i.e. an empty string exists between the opening and closing tags of the div ). (?!<\/?div>). the above example will verify that the next char should not be the starting char in <div> or </div> , but this fails, since the next char is < , which is the looking char in </div> . Since we defined this particular regular expression to repeat zero or more times, (?:(?!<\/?div>).)*? , it captures the intermediate empty string.