Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 787601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:13:44+00:00 2026-05-14T21:13:44+00:00

So I have an xml file with the following simplified xml file contents: <CollectionItems>

  • 0

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:

  1. I was attempting to modify the file in a text editor for simplicity.
  2. 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!

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:13:45+00:00Added an answer on May 14, 2026 at 9:13 pm

    You could use

    <CollectionItem>((?!<CollectionItem>).)+?<SubElement2 />.+?</CollectionItem>
    

    This ensures that no further <CollectionItem> comes between the starting tag and the <SubElement2 /> tag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml file with following contents: <Xavor> <Dev> <Emp>1</Emp> <Floor>1</Floor> <Salary>1200.4</Salary> </Dev>
I have the following xml file for navigation bar layout: navigation_bar.xml EDIT: I simplified
I have following xml file: <ab> <![CDATA[ <table> <tbody> <tr> <th>abcdef</th> </tr> <tr> <p>
I have a XML file with the following data format: <net NetName=abc attr1=123 attr2=234
I have an xml file in the following format: <food> <desert> cake <desert> </food>
I have the following xml file: <xfa:data> <form1> <Page1> <Page2> <contractInfo> ... </contractInfo> <paymentInfo>
I have a xml file main.xml with following markup and data. main.xml <xml> <content>
I have a large XML file which in the middle contains the following: <ArticleName>Article
I have a large xml file (approx. 10 MB) in following simple structure: <Errors>
I have a class which loads an xml file using the following: Path.Combine( AppDomain.CurrentDomain.BaseDirectory,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.