So I have an xml file with the following simplified xml file contents:
<CollectionItems>
<CollectionItem>
<Element1>Value1</Element1>
<Element2>
<SubElement1>SubValue1</SubElement1>
<SubElement2>SubValue2</SubElement2>
<SubElement3>SubValue3</SubElement3>
</Element2>
<Element3>Value3</Element3>
</CollectionItem>
<CollectionItem>
<Element1>Value1</Element1>
<Element2>
<SubElement1>SubValue1</SubElement1>
<SubElement2 />
<SubElement3>SubValue3</SubElement3>
</Element2>
<Element3>Value3</Element3>
</CollectionItem>
<CollectionItem>
<Element1>Value1</Element1>
<Element2>
<SubElement1>SubValue1</SubElement1>
<SubElement2>SubValue2</SubElement2>
<SubElement3>SubValue3</SubElement3>
</Element2>
<Element3>Value3</Element3>
</CollectionItem>
</CollectionItems>
I am attempting to write a regex in .Net which matches any CollectionItem where SubElement2 is empty (the middle CollectionItem in this example).
I have the following regex so far (SingleLine mode enabled):
<CollectionItem>.+?<SubElement2 />.+?</CollectionItem>
The problem is that it is matching the opening of the first CollectionItem through the close of the second CollectionItem. I understand why it's doing this, but I don't know how to modify the regex to make it match only the center CollectionItem.
Edit: As to why regex as opposed to something else:
- I was attempting to modify the file in a text editor for simplicity.
- After I couldn't figure out how to do it in regex, I wanted to know if it could be done (and how) for the sake of learning.
Thanks!