The Java Program: FetchRegex.java

  1 import java.io.IOException;
  2 import java.io.Reader;
  3 import java.io.BufferedReader;
  4 import java.io.InputStreamReader;
  5 import java.net.URL;
  6 import java.net.URLConnection;
  7 import java.util.Scanner;
  8 import java.util.regex.*;
  9 import java.io.*;
 10 
 11 public final class FetchRegex {
 12 
 13    private static String linkName (String cpid) {
 14       return String.format ("http://www.usaco.org/index.php?page=viewproblem&cpid=%s&lang=en", cpid);
 15    }
 16 
 17    public final static void main (String[] args) throws IOException{
 18       final String url = linkName (args[0]);
 19       final URLConnection connection = new URL (url).openConnection();
 20       String xml = new Scanner(connection.getInputStream()).useDelimiter("\\A").next();
 21 
 22       final String string = "<h2>\\s*(.*?)\\s*</h2>.*?<span.*?>\\s*(.*?)\\s*</span>";
 23 
 24       final Pattern pattern = Pattern.compile (string, Pattern.DOTALL);
 25 
 26       final Matcher matcher = pattern.matcher(xml);
 27       final boolean matchFound = matcher.find();
 28       if (matchFound) {
 29          final String title = matcher.group(1);
 30          System.out.println (title);
 31          System.out.println ();
 32          final String span = matcher.group(2);
 33          System.out.println (span);
 34       }
 35 
 36    }
 37 
 38 
 39 
 40 }