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

  • Home
  • SEARCH
  • 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 6818939
In Process

The Archive Base Latest Questions

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

I need to scrape some content from a HTTP response with Java. The required

  • 0

I need to scrape some content from a HTTP response with Java. The required fields in the response are: foo, bar and bla. My current pattern is very slow. Any ideas how to improve that?

Response:

...
<div class="ui-a">
<div class="ui-b">
    <p><strong>foo</strong></p>
    <p>bar</p>
</div>
<div class="ui-c">
    <p><strong>bla</strong></p>
    <p>...</p>
</div>
</div>

<div class="ui-a">
<div class="ui-b">
    <p><strong>foo1</strong></p>
    <p>bar1</p>
</div>
<div class="ui-c">
    <p><strong>bla1</strong></p>
    <p>...</p>
</div>

Pattern:

.*?<div class="ui-a">.*?<strong>(.*?)</strong>.*?<p>(.*?)</p>.*?</div>.*?<div class="ui-c">.*?<strong>(.*?)</strong>.*?
  • 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-26T21:14:21+00:00Added an answer on May 26, 2026 at 9:14 pm

    Since you can’t make use of an HTML parser, try something like this:

    import java.util.regex.*;
    
    public class Main {
        public static void main (String[] args) {
            String html =
                    "...\n" +
                    "<div class=\"ui-a\">\n" +
                    "<div class=\"ui-b\">\n" +
                    "    <p><strong>foo</strong></p>\n" +
                    "    <p>bar</p>\n" +
                    "</div>\n" +
                    "<div class=\"ui-c\">\n" +
                    "    <p><strong>bla</strong></p>\n" +
                    "    <p>...</p>\n" +
                    "</div>\n" +
                    "</div>\n" +
                    "\n" +
                    "<div class=\"ui-a\">\n" +
                    "<div class=\"ui-b\">\n" +
                    "    <p><strong>foo1</strong></p>\n" +
                    "    <p>bar1</p>\n" +
                    "</div>\n" +
                    "<div class=\"ui-c\">\n" +
                    "    <p><strong>bla1</strong></p>\n" +
                    "    <p>...</p>\n" +
                    "</div>";
    
            Pattern p = Pattern.compile(
                    "(?sx)                               # enable DOT-ALL and COMMENTS     \n" +
                    "<div\\s+class=\"ui-a\">             # match '<div...ui-a...>'         \n" +
                    "(?:(?!<strong>).)*+                 # match everything up to <strong> \n" +
                    "<strong>([^<>]++)</strong>          # match <strong>...</strong>      \n" +
                    "(?:(?!<p>).)*+                      # match up to <p>                 \n" +
                    "<p>([^<>]++)</p>                    # match <p>...</p>                \n" +
                    "(?:(?!<div\\s+class=\"ui-c\">).)*+  # match up to '<div...ui-a...>'   \n" +
                    "<div\\s+class=\"ui-c\">             # match '<div...ui-c...>'         \n" +
                    "(?:(?!<strong>).)*+                 # match everything up to <strong> \n" +
                    "<strong>([^<>]++)</strong>          # match <strong>...</strong>      \n"
            );
    
            Matcher m = p.matcher(html);
    
            while(m.find()) {
                System.out.println("---------------");
                for(int i = 1; i <= m.groupCount(); i++) {
                    System.out.printf("group(%d) = %s\n", i, m.group(i));
                }
            }
        }
    }
    

    which will print the following to the console:

    ---------------
    group(1) = foo
    group(2) = bar
    group(3) = bla
    ---------------
    group(1) = foo1
    group(2) = bar1
    group(3) = bla1

    Note my changes:

    • *+ and ++: http://www.regular-expressions.info/possessive.html
    • instead of .*?, I used (?:(?!...).)*+. The first, .*? will keep track of all possible matches it makes to be able to back-track at a later stage. The latter, (?:(?!...).)*+, will not keep track of these matches.

    That should make it quicker (not sure by how much…).

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

Sidebar

Related Questions

I need to scrape some data from webpages. But I have some encoding problems
I need to scrape some websites, and would like to avoid downloading images from
I need a javascript function to automatically login and then scrape some detail from
I need to scrape an xml file from http://feeds.feedburner.com/Torrentfreak for its links and description.
I need to scrape Form 10-K reports (i.e. annual reports of US companies) from
Iam building a scraper which needs to scrape some web content. Iam facing an
I am trying to scrape some data from a page with a table based
I want to scrape string data from some binary text files that contain embedded
I need to scrape from a website that requires authentication, that is a user
I am trying to scrape some content (am very new to Python) and I

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.