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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:24:52+00:00 2026-05-22T02:24:52+00:00

i have a serious problem. i would like to extract the content from tag

  • 0

i have a serious problem.
i would like to extract the content from tag such as:

<div class="main-content">
    <div class="sub-content">Sub content here</div>
      Main content here </div>

output i would expect is:

Sub content here
Main content here

i’ve tried using regex, but the result isn’t so impressive.
By using:

Pattern.compile("<div>(\\S+)</div>");

would return all the strings before the first <*/div> tag
so, could anyone help me pls?

  • 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-22T02:24:53+00:00Added an answer on May 22, 2026 at 2:24 am

    I’d recommend avoiding regex for parsing HTML. You can easily do what you ask by using Jsoup:

    public static void main(String[] args) {
        String html = "<html><head/><body><div class=\"main-content\">" +
                "<div class=\"sub-content\">Sub content here</div>" +
                "Main content here </div></body></html>";
        Document document = Jsoup.parse(html);
        Elements divs = document.select("div");
        for (Element div : divs) {
            System.out.println(div.ownText());
        }
    }
    

    In response to comment: if you want to put the content of the div elements into an array of Strings you can simply do:

        String[] divsTexts = new String[divs.size()];
        for (int i = 0; i < divs.size(); i++) {
            divsTexts[i] = divs.get(i).ownText();
        }
    

    In response to comment: if you have nested elements and you want to get own text for each element than you can use jquery multiple selector syntax. Here’s an example:

    public static void main(String[] args) {
        String html = "<html><head/><body><div class=\"main-content\">" +
                "<div class=\"sub-content\">" +
                "<p>a paragraph <b>with some bold text</b></p>" +
                "Sub content here</div>" +
                "Main content here </div></body></html>";
        Document document = Jsoup.parse(html);
        Elements divs = document.select("div, p, b");
        for (Element div : divs) {
            System.out.println(div.ownText());
        }
    }
    

    The code above will parse the following HTML:

    <html>
    <head />
    <body>
    <div class="main-content">
    <div class="sub-content">
    <p>a paragraph <b>with some bold text</b></p>
    Sub content here</div>
    Main content here</div>
    </body>
    </html>
    

    and print the following output:

    Main content here
    Sub content here
    a paragraph
    with some bold text
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok guys, I have a serious problem with this. I have a static class
I have the following problem: I would like to visualize a discrete and a
I have a serious problem. One of our servers crashed. Now I reinstalled the
I have a serious performance problem. I have a database with (related to this
I have a serious problem with calculating Standard Deviation within subgroup in SSRS/SQl (whichever
Problem Statement: I would like to create an offline database to lookup prices/info on
I am completely new to python and I have a serious problem which I
I have a NVida graphic card and I would like to use it for
I Have a really serious Problem on my end: Im Making an Audio Synchronized
Here's the situation: I have a web-based ticket application, multiple users. One problem that

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.