Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Java regex problem

    Hi all,

    The code below takes a URL and prints out the source code. However I need the code to use a regulary expression to match a 'a href' links in the html and print these out. I need to change the code below in order to do this but I am not sure how. Please help...


    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 GetURLInfo
    {

    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());

    while (in.hasNextLine()) {
    System.out.println(" " + in.nextLine());
    }

    Pattern regex = Pattern.compile("<a href.*?>");
    while(in.hasNext(regex)) {
    System.out.println(in.next(regex));
    }
    }

    // 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);
    }
    }

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Code:
    Matcher m = Pattern.compile("<a href=(['\"])(.*?)\\1").matcher(s);
    while ( m.find() ) {
      System.out.println(m.group(2));
    }
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •