segunda-feira, 24 de janeiro de 2011

Exemplo de Expressão Regular

public static void main(String[] args){
String entrada;
String endereco;
String padrao="href=\"[a-zA-Z.0-9/:_+\-]*.jpg";
padrao="h[a-zA-Z.0-9/:_+\-]*-h/[a-zA-Z.0-9/:_+\-]*.png";
FileInputDemo aux = new FileInputDemo();
File testFile = new File("c:/Aulas/RegEx/pagina_julho_2010a.txt");
entrada = aux.getContents(testFile);
URLConnectionReader auxUrl = new URLConnectionReader();
Pattern pattern = Pattern.compile(padrao);

Matcher matcher = pattern.matcher(entrada);

boolean found = false;
while (matcher.find()) {
endereco = matcher.group().substring(6);
try {
System.out.println(endereco);
// auxUrl.urlRead(endereco);
} catch (Exception ex) {
Logger.getLogger(RegEx.class.getName()).log(Level.SEVERE, null, ex);
}
found = true;
}
if(!found){
System.out.printf("No match found.%n");
}
}
}