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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:04:02+00:00 2026-05-23T10:04:02+00:00

I checked for this specific question and could not find any. I am writing

  • 0

I checked for this specific question and could not find any. I am writing a program in Java which analyses content from web pages, so I need a regular expression which can weed out all the links and tags (href, img, etc…), so that I could display only the pure content written and visible in the webpages. Thanks a lot.

Hi I wanted to make it more specific:

URLConnection connection = wordURL.openConnection("http://en.wikipedia.org/wiki/Bloom_filter");
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
String word = "bloom filter";
String regexp2 = word; 
Pattern pattern2 = Pattern.compile(regexp2);
String HTML_REGEX = "(<.+?>)+"; // as per your answer(Martijn Courteaux)
while ((line = br.readLine()) != null)
{
       String content;
       if ( (content = line.replaceAll(HTML_REGEX, "\n") )!= null)
       {
              Matcher matcher2 = pattern2.matcher(line);
              if(matcher2.find())
              {
                   System.out.println(line);
              }
        }
 }

But unfortunately it still prints out paragraph (<p>) tag and also <li> tag with some rubbish inside </li>. I would like to restrict it to display only those words where “bloom filter” is present.Thanks again.

  • 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-23T10:04:02+00:00Added an answer on May 23, 2026 at 10:04 am

    I really know it isn’t good to use a regex with html. But if he really wants to this might help:

    String HTML_REGEX = "<.+?>";
    String yourHTML = "<html><body><h1>Lorem Ipsum</h1>" + 
                      "<p>Lorem <i>Ipsum</i> dolorem sedet. Set nihil amat. " + 
                      "<sub>I don't know the text</sub></p></body></html>"
    
    String content = yourHTML.replaceAll(HTML_REGEX, "\n");
    System.out.println(content);
    

    prints:

    
    
    
    Lorem Impsum
    
    Lorem 
    Ipsum
    dolorem sedet. Set nihil amat. 
    I don't know the text
    
    
    
    
    

    As you can see, it will work, but it is definitely not what you want.


    You can reduce the number of newlines by using this regex:

    String HTML_REGEX = "(<.+?>)+";
    String yourHTML = "<html><body><h1>Lorem Ipsum</h1>" + 
                      "<p>Lorem <i>Ipsum</i> dolorem sedet. Set nihil amat. " + 
                      "<sub>I don't know the text</sub></p></body></html>"
    
    String content = yourHTML.replaceAll(HTML_REGEX, "\n");
    System.out.println(content);
    

    prints:

    
    Lorem Impsum
    Lorem 
    Ipsum
    dolorem sedet. Set nihil amat. 
    I don't know the text
    
    

    I tried your code, and it didn’t work indeed. After some editing this worked:

    URLConnection connection = new URL("http://en.wikipedia.org/wiki/Bloom_filter").openConnection();
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;
    String word = "bloom filter".toLowerCase();
    String HTML_REGEX = "(<.+?>)+"; // as per your answer(Martijn Courteaux)
    while ((line = br.readLine()) != null) {
        String content;
        if ((content = line.replaceAll(HTML_REGEX, "\n")) != null) {
            if (content.toLowerCase().contains(word)) /* Changed: regex match -> contains() */
            {
                System.out.println(content); /* CHANGED: line -> content */
            }
        }
    }
    

    What you did wrong was:

    1. You didn’t print content, but line which of course contains the tags…
    2. You tried to find the word “bloom filter” using a regex, which is case sensitive. So, just lowercase the strings and use String.contains(CharSequence target), which tells you if the target string is a part of the whole string.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The closest answer I could find to my question here was this (which has
I am calling a WebMethod from this code: if($(this).attr(checked)) { .. MyWebMethod(variable1, variable2, onSuccessFunction);
(The context of this question is specific to XNA, but the principle is general.)
this is my first question here @stackoverflow. I'm writing a monitoring tool for some
This is mostly a rhetorical question, as far as I've checked the answer is
This seems to be a frequent question around here, but no specific answer. I've
Duplicate: This is the exact same question, from the exact same person, as Issue
I am having the same issues as per this question: WCF not deserializing JSON
I have a 2884765579 bytes file. This is double checked with this function, that
$('input[type=checkbox]').unbind().click(function(e){ $(this).attr('checked', true) return false; }); I NEED to return false because I have

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.