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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:29:11+00:00 2026-05-12T06:29:11+00:00

Greetings! I have some HTML that may or may not be valid. If the

  • 0

Greetings!

I have some HTML that may or may not be valid. If the HTML is invalid, a best attempt can be made, and any errors that arise are acceptable (ie, grouping too much because some tag isn’t closed correctly).

In this HTML are a variety of elements, some of which may have a class (call it “findme”). These elements are of varying type; some img, some object, some a, etc.

I need a regex that will pull out all the elements, and the content they contain if they contain content.

For example:

<div>
<span><img class="findme" src="something" /></span>
<object class="findme" classid="clsid:F08DF954-8592-11D1-B16A-00C0F0283628" id="Slider1" width="100" height="50">
  <param name="BorderStyle" value="1" />
  <param name="MousePointer" value="0" />
  <param name="Enabled" value="1" />
  <param name="Min" value="0" />
  <param name="Max" value="10" />
</object>
</div>

Running the regex on that chunk of HTML should return 2 elements:

<img class="findme" src="something" />

and

<object class="findme" classid="clsid:F08DF954-8592-11D1-B16A-00C0F0283628" id="Slider1" width="100" height="50">
      <param name="BorderStyle" value="1" />
      <param name="MousePointer" value="0" />
      <param name="Enabled" value="1" />
      <param name="Min" value="0" />
      <param name="Max" value="10" />
    </object>

Any of you regex gurus out there have an idea on this one?

Edit:
The language is c#.

  • 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-12T06:29:11+00:00Added an answer on May 12, 2026 at 6:29 am

    While regular expressions can be good for a large variety of tasks, I find it usually falls short when parsing HTML DOM. The problem with HTML is that the structure of your document is so variable that it is hard to accurately (and by accurately I mean 100% success rate with no false positive) extract a tag.

    What I recommend you do is use a DOM parser such as SimpleHTML and use it as such:

    require_once('SimpleHTML.class.php')
    
    $html_dom = str_get_dom($html);
    $tags = $html_dom->find('img.findme'); // Get all img elements of class findme
    

    Some may think this is overkill, but in the end, it will be easier to maintain and also allows for more extensibility. For example, using the DOM parser, I can also get the alt attribute.

    A regular expression could be devised to achieve the same goal but would be limited in such way that it would force the alt attribute to be after the src or the opposite, and to overcome this limitation would add more complexity to the regular expression.

    Also, consider the following. To properly match an <img> tag using regular expressions and to get only the class attribute (captured in group 2), you need the following regular expression:

    <\s*img\s+[^>]*?\s*class\s*=\s*(["'])((\\?+.)*?)\1[^>]*?>
    

    And then again, the above can fail if:

    • The attribute or tag name is in capital and the i modifier is not used.
    • Quotes are not used around the class attribute.
    • Another attribute then class uses the > character somewhere in their value.
    • Some other reason I have not foreseen.

    So again, simply don’t use regular expressions to parse a dom document.

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

Sidebar

Related Questions

Greetings, Apologies in advance that I have not researched this toughly enough to answer
Greetings, I have an asp.net mvc application. I have some links that corresponds to
Greetings! I have some XML like this: <Root> <AlphaSection> . . . </AlphaSection> <BetaSection>
Greetings! I have some XML like this: <Root> <MainSection> <SomeNode>Some Node Value</SomeNode> <SomeOtherNode>Some Other
Greetings: I have put together a RESTful web service in .NET 3.5 that takes
Greetings, I have a particular object which can be constructed from a file, as
Greetings! I have an XML value that I'd like to use as a boolean
Greetings. I have some inputs dynamically added to form. <input name=input_names[] /> When form
Greetings, everyone. I'm trying to learn some Silverlight basics, and have decided to write
Greetings! I've been trying to display some HTML with Java using JEditorPane . But

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.