Positive look behind with regex is another useful constraint that we can add to our expression. It only matches the right side, if and only if the look behind matches. The look behind doesn’t consume any characters and has the following syntax:

(?<=<look behind>)<right side>

Example:

<?xml version="1.0"?>
<fruits>
 <a>apple</a>
 <b></b>
 <c>cashew</c>
 <d></d>
</fruits>

From the XML above find all the empty elements and add an attribute empty that is set to true but only matching the opening tag.

Find Replace Comment
(?<=\n\s)<(?<tag>\w*[^>])>(?=<) <$1 empty=”true”> The positive <look behind> is the one highlighted in blue.

The <right side> is the one highlighted in green.