Duvida Regex

3 respostas
xandevieira

Estou tentando pegar somente o conteudo do href
Testei varias formas mas não estou conseguindo.

Ex. estou lendo uma pagina da web e salvando o conteudo num stringbuinder

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head> 
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="ROBOTS" content="NOINDEX,NOFOLLOW">
<title>CADASTRE-SE ABAIXO - PARA CONTINUAR!</title> 
<link rel="stylesheet" href="http://www.freetela.com/link/files/style1.css" type="text/css" />
<link rel="stylesheet" href="http://www.freetela.com/link/files/style2.css" type="text/css" />
<link rel="stylesheet" href="http://www.freetela.com/link/files/style3.css" type="text/css" />
<!--[if IE]><link rel="stylesheet" href="http://www.yesdicas.net/link/files/ie.css" type="text/css" /><![endif]--> 
<div id="link0" style="display:block"> 
        <a   ><img src="http://2.bp.blogspot.com/_Pu4QmVBO8sw/TUMRe6jhYTI/AAAAAAAAB7Q/mWhXOFpqRy4/s1600/assistir01.gif"    ></a>
        &lt;/div&gt;
        &lt;div id="link" style="display:none"&gt;
        <a href="http://www.megaupload.com/?d=98FJMA4T"    >&lt;span class="left"&gt;&lt;span class="right"&gt;&lt;span class="center center_auto"&gt;&lt;span class="icon icon_download"&gt;<img src="http://2.bp.blogspot.com/_Pu4QmVBO8sw/TUMRk2FlTTI/AAAAAAAAB7Y/YFkvrqPYmzg/s1600/assistir02.gif"    >&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;</a>



&lt;/div&gt;

E gostaria de pegar somente os links
http://www.megaupload.com/?d=98FJMA4T
...
...
http://www.yesdicas.net/link/files/ie.css
http://www.freetela.com/link/files/style1.css

2 das minhas tentativas

(?=(href="))[^"]*
[0-9]*"[ >]*[\w ]*

href="*.*"

este foi o mais proximo do que quero
a href=\"(.*?)\">

3 Respostas

bruno.fantin

Sempre que tenho que pegar algo de um html eu uso o htmlparser (http://htmlparser.sourceforge.net/), acho bem mais confiante.

Agora sobre seu problema de regex, eu não sei.

Falou.

A

Tente isso:

String conteudo = ""; //seu texto html
        Pattern pattern = Pattern.compile("href\\=\\\"([^\\\"]+)\\\"");                
        Matcher matcher = pattern.matcher(conteudo);
        while( matcher.find() ) {
            System.out.println( matcher.group(1) );
        }
xandevieira

AbelBueno:
Tente isso:

String conteudo = ""; //seu texto html Pattern pattern = Pattern.compile("href\\=\\\"([^\\\"]+)\\\""); Matcher matcher = pattern.matcher(conteudo); while( matcher.find() ) { System.out.println( matcher.group(1) ); }

vlw, é isso mesmo que precisava

Criado 4 de julho de 2011
Ultima resposta 4 de jul. de 2011
Respostas 3
Participantes 3