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 495865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:37:55+00:00 2026-05-13T05:37:55+00:00

I am trying to match elements that have no other children elements, but also

  • 0

I am trying to match elements that have no other children elements, but also have content. No content also includes whitespace and   characters. I need to do this in C#.

Take this XML for instance:

<1>
    <2><3 /></2>
    <4>
        <5>This is match 1</5>
    </4>
    <6>     
         </6>
    <7>    &nbsp;&nbsp;&nbsp;&nbsp;    &nbsp;&nbsp;&nbsp;</7>
    <8>This is match 2</8>
</1>

So only elements 5 and 8 match. The rest of the elements have child elements or “whitespace” (spaces, tabs, carriage returns, new lines, &nbsp;)

Note

SLaks posted:

“In general, you must not parse XML using regular expressions.
Instead, use the System.Xml namespace.”

This unfortunately is not viable in this situation. This is an application that was not made by my team and we need to optimize it without rewriting anything (not my decision). It is invalid XML and so I need to do this in order to make it valid. Then I can treat it as xml 🙂

So in other words, it is a string that closely resembles XML.

This is what I have come up with so far, it accounts for everything but the “whitespace” exclusion:

  Regex ElementExpression = new Regex(
      @"<(?'tag'\w+?).*>" + // match first tag, and name it 'tag'
      @"(?'text'[^<>]*[\\S]+?)" + // match text content, name it 'text'
      @"</\k'tag'>" // match last tag, denoted by 'tag'
      , RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
  • 1 1 Answer
  • 0 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-13T05:37:55+00:00Added an answer on May 13, 2026 at 5:37 am

    I was able to get what I wanted by using one regex to get the elements and a second regex to remove the ones with the whitespace I defined.

    With about 30MB of data it takes 3 seconds.

      Regex ElementExpression = new Regex(
                @"<(?'tag'\w+?)(?'attributes'.*?)>" + // match first tag, and name it 'tag'
                @"(?'text'[^<>]*?)" + // match text content, name it 'text'
                @"</\k'tag'>" // match last tag, denoted by 'tag'
                , RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
    
    
      Regex WhiteSpaceExpression = new Regex(@"\A((&nbsp;)|(\s)|(\r))*\Z", RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
    
      text = ElementExpression.Replace(text, delegate(Match match){
            if (match.Groups.Count > 0){
               Group textGroup = match.Groups["text"];
               if (!WhiteSpaceExpression.IsMatch(textGroup.Value)){
                  return String.Format("<{0}{1}>{2}</{0}>", match.Groups["tag"].Value, match.Groups["attributes"].Value, HttpUtility.HtmlEncode(textGroup.Value));
               }
               else{
                  return String.Format("<{0}{1} />", match.Groups["tag"].Value, match.Groups["attributes"].Value);
               }
            }
            return match.Value;
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to match elements with a name that is 'container1$container2$chkChecked' , using a
I'm trying to get a regex that will match: somefile_1.txt somefile_2.txt somefile_{anything}.txt but not
Here I am trying to filter only the elements that do not have a
Medicare Eligibility EDI Example Responses is what I'm trying to match. I have a
I have a URL , and I'm trying to match it to a regular
I'd trying to style my ComboBoxes to match the rest of the UI but
I'm trying to come up with a Java regex that will match a filename
I'm trying to find a jQuery selector that will match only the element that
Here's what I'm trying to do: I have a bookmarklet that is looking for
I want to remove the class attribute from all elements that have an empty

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.