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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:19:18+00:00 2026-05-26T09:19:18+00:00

i have some text which I know and I need to find first tag

  • 0

i have some text which I know and I need to find first tag with class defined around that text.

Example:

<table>
  <td class="foo">
    <p>...</p>
  </td>
  <td class="bar">
    <p>Text i dont know</p>
    <p>Text i know</p>
    <p>Text i dont know</p>
  </td>
</table>

I tried a lot of things. I know how to find a closing tag, but when I try to find opening tag, my regex returns td with class “foo” instead one with class “bar”.

I will really appreciate your help.

Edit:
I want to do it in python. I provided weak specification of the problem. That tag dont have to be tag, it can be any tag with class specified. I dont want to “parse” html with regex, but i dont see any other way how to do such thing without using regular expressions.
What i need is to find first tag around that tag which have class specified.

  • 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-26T09:19:19+00:00Added an answer on May 26, 2026 at 9:19 am

    Okay, here we go!

    (?s)<(\w+)[^>]*\sclass="[^"]*"[^>]*>(?:(?!</?\1\b|<\w+[^>]*\sclass="[^"]*"[^>]*>)(?:Text i know()|.))*</\1>\2
    

    The usual way to match just one of an element whose name is not know in advance is (we’ll assume the (?s) from here on):

    <(\w+)[^>]*>(?:(?!</?\1\b).)*</\1>
    

    The lookahead – (?!</?\1\b) – prevents the dot from matching if it happens to be the first character of a tag (opening or closing) with the same name as the element you’re currently matching. In this case a class attribute is required too, so the first part becomes:

    <(\w+)[^>]*\sclass="[^"]*"[^>]*>
    

    The question wasn’t prefectly clear on this, but I’m assuming you want to match the most immediate enclosing element with a class attribute. That is, in the following text, you want to match the td.yes-me element, not the table element.

    <table class="not-me">
      <td class="not-me-either">
        <p>Text i dont know</p>
      </td>
      <td class="yes-me">
        <p>Text i dont know</p>
        <p>Text i know</p>
        <p>Text i dont know</p>
      </td>
      <td>
        <p>Text i dont know</p>
      </td>
      <td>
        <p>Text i dont know</p>
        <p>Text i know</p>
        <p>Text i dont know</p>
      </td>
    </table>
    

    That means the lookahead also has to exclude any opening tag with a class attribute. It now grows into this:

    (?!</?\1\b|<\w+[^>]*\sclass="[^"]*"[^>]*>)
    

    And finally, the element’s content should include your target text (Text i know). After the lookahead succeeds, we try to match that; if we succeed, the empty capturing group following it captures an empty string. Otherwise the dot consumes the next character and the process repeats.

    When it’s all done matching and the closing tag has been matched, the backreference \2 confirms that the target text was seen. Since that group didn’t consume any characters, the backreference doesn’t either, but it still reports success if the group participated in the match.

    Back-assertions (as I like to call them) don’t work in all flavors, and aren’t officially supported in any of them, but they work in most of the Perl-derived flavors, including Python. (The most notable exceptions are JavaScript and other ECMAScript implementations.)

    If you’re reaction to this answer is abject horror, don’t worry, I’m not offended. 😉 Inspiring you to search harder for a solution that doesn’t involve regexes is a successful outcome, too. (But it does work!)

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

Sidebar

Related Questions

I have some xml files that contain text, which are displayed on my website.
I have a JeditorPane which has some text in HTML format. When I execute
I have a .rc file which is used to include some text data in
I have some client-side validation against a text box, which only allows numbers up
I have a script which collects an image, link and some text content from
I have a custom UITableViewCell which has an image & and some text. When
I have a line of Fortran code, which includes some text. I'm changing the
I have a web page with a read-only text box which shows some HTML
I have some text/images that I only want to display in the normal state
For example, we have this xml: <x> <y>some text</y> <y>[ID] hello</y> <y>world [/ID]</y> <y>some

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.