Ver Mensaje Individual
  #4 (permalink)  
Antiguo 12/10/2011, 08:27
Avatar de DeeR
DeeR
 
Fecha de Ingreso: diciembre-2003
Ubicación: Santiago
Mensajes: 520
Antigüedad: 20 años, 4 meses
Puntos: 17
Respuesta: Expresiones regulares

kh0d3x

Una vez que le entregas al matchet el pattern, debes utilizar el método find para encontrar las coincidencias, si deseas tener todas las coincidencias, puedes usar un while.

Te dejo un ejemplo funcional

- Extración del contenido (String) de una url
- Extración de datos utilizando expresiones regulares


Código JAVA:
Ver original
  1. package extraerdatos;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.util.regex.*;
  6.  
  7. /**
  8.  * @author deerme.org
  9.  */
  10. public class GetPlayers
  11. {
  12.     private String url_site;
  13.     private String content;
  14.    
  15.     public GetPlayers( String url )
  16.     {
  17.         this.url_site = url;
  18.         this.content = "";
  19.     }
  20.     public Boolean GetContent()
  21.     {
  22.         String strContents;
  23.          try
  24.          {
  25.             // Get Content
  26.             byte[] buff = new byte[4096];
  27.             URL url = new URL( this.url_site );
  28.             BufferedInputStream bis = new BufferedInputStream(url.openStream());
  29.  
  30.             int bytesRead = 0;
  31.             while ((bytesRead = bis.read(buff)) != -1)
  32.             {
  33.                 strContents = new String(buff, 0, bytesRead);
  34.                 this.content = this.content + strContents;
  35.             }
  36.             bis.close();
  37.             return true;
  38.         } catch (Exception ex)
  39.         {
  40.             return false;
  41.         }
  42.    
  43.     }
  44.     public void ExtractData()
  45.     {
  46.         int i; int nresult=0;
  47.         if ( !this.content.equals("") )
  48.         {
  49.             Pattern pt = Pattern.compile("<tr class=\"odd\"><td>(.*?)</td><td><a href=\".*?\">(.*?)</a></td><td><a href=\".*?\">(.*?)</a></td><td>(.*?)</td><td>(.*?)</td><td>(.*?)</td> </tr>");
  50.             Matcher mt = pt.matcher(  this.content );
  51.             System.out.println("Equipo\tPosición\tJugador\tPuntos");
  52.             while(  mt.find() == true)
  53.             {
  54.                 System.out.println( mt.group(3) + "\t" + mt.group(1) + "\t" + mt.group(2) + "\t" + mt.group(4)  );
  55.             }
  56.         }
  57.     }
  58. }




Código JAVA:
Ver original
  1. // En mi caso utilizo un proxy para salir a la red
  2.  
  3. public class Main {
  4.    
  5.     public static void main(String[] args)
  6.     {
  7.         System.setProperty("http.proxyHost", "localhost");
  8.         System.setProperty("http.proxyPort", "8889");
  9.  
  10.         GetPlayers pl = new GetPlayers("http://www.nomaskeine.com/estadisticas/puntos/25/mejores/jugadores");
  11.         pl.GetContent();
  12.         pl.ExtractData();
  13.     }
  14.  
  15. }

Última edición por DeeR; 12/10/2011 a las 08:29 Razón: :P