Regex problem
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   martin_cooke
Posted On:   Monday, December 1, 2008 11:25 AM

Why wont this program print out only its regular expression given. The program takes a URL and then should print out only certain information from the source code of the URL. At the moment it only prints out the source code and doesn't seem to match the criteria set in my regular expression. Any one know why? import java.net.*; import java.io.*; import java.util.*; import javax.swing.JOptionPane; import java.util.regex.Matcher; import java.util.regex.Pattern; public class printOutahref { public void printinfo(URLConnection u) throws IOException { //set host, port System.getProperties().put( "pr   More>>

Why wont this program print out only its regular expression given.


The program takes a URL and then should print out only certain information from the source code of the URL.


At the moment it only prints out the source code and doesn't seem to match the criteria set in my regular expression.


Any one know why?



			


import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class printOutahref
{

public void printinfo(URLConnection u) throws IOException
{
//set host, port
System.getProperties().put( "proxySet", "true" );
System.getProperties().put( "proxyHost", "proxysg.uwe.ac.uk" );
System.getProperties().put( "proxyPort", "80" );

// Display the URL address, and information about it.
System.out.println(u.getURL().toExternalForm() + ":");
System.out.println(" Content Type: " + u.getContentType());
System.out.println(" Content Length: " + u.getContentLength());
System.out.println(" Last Modified: " + new Date(u.getLastModified()));
System.out.println(" Expiration: " + u.getExpiration());
System.out.println(" Content Encoding: " + u.getContentEncoding());

//Scanner in = new Scanner(u.getInputStream());

InputStreamReader in = new InputStreamReader(u.getInputStream()); // this replaces Scanner

StringBuilder document = new StringBuilder();
char[] buffer = new char[1024]; // read a kilobyte per read() operation
int read;
while ((read = in.read(buffer)) > 0)
document.append(buffer, 0, read);

// define a regex to search for
String regex = "

(.*?)

";
Pattern p = Pattern.compile(regex);
Matcher m;

// for (String line : document) {
m = p.matcher(document); // get a matcher object
while (m.find()) {
// group is match in regex in ( ) 's
System.out.println(m.group(1));
}


in.close();
}

// Create a URL from the specified address, open a connection to it,
// and then display information about the URL.
public static void main(String[] args)
throws MalformedURLException, IOException {

//get URL as string
String URLname = JOptionPane.showInputDialog("Enter a URL");
//construct URL
URL url = new URL(URLname);
//open connection
URLConnection connection = url.openConnection();
//call print info for our URL
(new GetURLInfo()).printinfo(connection);
}
}

   <<Less
About | Sitemap | Contact