Negative look ahead with regex is another useful constraint that we can add to our expression. It only matches the left side, if and only if the look ahead doesn't match. The look ahead doesn't consume any characters and has the following syntax:

<left side>(?!<look ahead>)

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
<(?<tag>\w*[^>])>(?!(\n|\w)) <$1 empty=”true”> The <left side> is the one highlighted in blue.

The negative <look ahead> is the one highlighted in green.