Search code examples
javagdata

get the following to return all urls in a text file that have a specific format


This is more on the code I posted earlier.

I have got the following to return a single URL contained within all the XML data I pulled from YouTube gdata. I am now having trouble getting it to return all the URLs that fall within the specified layout.

This is the code that returns a single URL

while ((inputLine = sc.findWithinHorizon("www.youtube.com/watch?v=9HJ2fl4zbx0&feature=youtube_gdata", 0)) != null)
    {
        System.out.println(inputLine);
    }

Now, I need it to return all URLs that start with "www.youtube.com/watch?v=" and end with "&feature=youtube_gdata"

Alternatively, it would work perfectly if I could have it return just the next 11 characters after "www.youtube.com/watch?v="


Solution

  • If I understand you, you could use something like

    Scanner sc = new Scanner(
            "www.youtube.com/watch?v=9HJ2fl4zbx0&feature=youtube_gdata");
    String openStr = "www.youtube.com/watch?v=";
    while (sc.hasNextLine() && (inputLine = sc.nextLine()) != null) {
        int pos = inputLine.indexOf(openStr);
        if (pos > -1) {
            pos += openStr.length();
            System.out.println(inputLine.substring(pos, pos + 11));
        }
    }
    

    Notice the above uses a single String Scanner, but the output is

    9HJ2fl4zbx0