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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:29:32+00:00 2026-05-16T20:29:32+00:00

I have Java class that have to fetch the content of an URL online

  • 0

I have Java class that have to fetch the content of an URL online (returning an XML), and apply a regexp over it (the behaviour is defined by third-party files, I so cannot use DOM or SAX to parse the response). Here is the code :

    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;


    public class RegExpTest {
        public static void main(String[] args) {
            try {
                StringBuffer buffer = new StringBuffer();
                URL url = new URL("http://api.themoviedb.org/2.1/Movie.search/en/xml/57983e31fb435df4df77afb854740ea9/Inglourious+Bastards");
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();

                conn.connect();
                InputStream input = conn.getInputStream();

                for(int c = input.read(); c != -1; c = input.read())
                    buffer.append((char)c);

                Pattern pattern = Pattern.compile("<movie>.*?<name>([^<]*)</name>.*?<id>([^<]*)</id>.*?</movie>", Pattern.DOTALL);
                Matcher matcher = pattern.matcher(buffer);
                for(int i = 1; i < (matcher.groupCount() + 1); i++) {
                    matcher.find();
                    String toReplace = matcher.group(i);
                    System.out.println(toReplace);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
}

Its output is for this sample “Inglourious Basterds” and then “22311”, which is the content of the name tag in the first movie tag, and the content of the id tag in the second movie tag. However, the lazy operator should guarantee that it is only the items in the first movie tag that are retrieved.

Moreover, the following code in python, which basically does exactly the same, works in the expected way.

import re
import urllib

url = urllib.urlopen("http://api.themoviedb.org/2.1/Movie.search/en/xml/"
    "57983e31fb435df4df77afb854740ea9/Inglourious+Bastards")

m = re.search("<movie>.*?<name>([^<]*)</name>.*?<id>([^<]*)</id>.*?</movie>",
        url.read(), re.DOTALL)
print m.group(1), m.group(2)

Any idea on what is wrong with my code ?

Thanks

  • 1 1 Answer
  • 1 View
  • 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-16T20:29:32+00:00Added an answer on May 16, 2026 at 8:29 pm

    There are two capturing groups in the regex, so find() gets called twice. You need to call find() (and check the result) before you start looking at the match info:

    while (matcher.find()) {
        for(int i = 1; i < (matcher.groupCount() + 1); i++) {
            String toReplace = matcher.group(i);
            System.out.println(toReplace);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on Mangler's Android implementation. I have a java class that looks
I have a simple Java class that I need to serialize to be stored
I have a simple Java class that has some methods: public class Utils {
I have a Java class MyPojo that I am interested in deserializing from JSON.
I have a quite huge Java class that has several imported packages and libraries
In Java I have the necessity of implementing a class that extends Thread within
I am new to Java. I just read that class variables in Java have
In Java, how can I have a Runnable class that, depending on its constructor,
Possible Duplicate: Difference between Color.red and Color.RED I have seen that the Java class
I'm using Java. I want to have a setter method of one class 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.