Como pegar o src url e href de um html

0 respostas
deFreitas

Pessoal ola,

Eu estou com um texto html e preciso pegar dele os links que estão nestes 3 atributos mencionados para isso eu tentei isso:

String texto2 = "url(\"primeiro url\")\n" +
    "url('2 url')\n" +
    "href=\"1 href\"\n" +
    "src=\"1 src\"\n" +
    "src='2 src'\n" +
    "url('3 url')\n" +
    "\n" +
    ".camera_target_content .camera_link {\n" +
    "   background: url(../images/blank.gif);\n" +
    "   display: block;\n" +
    "   height: 100%;\n" +
    "   text-decoration: none;\n" +
    "}";

    String exp = "(?:href|src)=[\"'](.+)[\"']+|(?:url)\\([\"']*(.*)[\"']*\\)";
    // expressão para pegar os links do src e do href
    Pattern pattern = Pattern.compile(exp);

    // preparando expressao
    Matcher matcher = pattern.matcher(texto2); 


    // pegando urls e guardando na lista
    while(matcher.find()) {


    System.out.println(texto2.substring(matcher.start(), matcher.end()));   


    }

Até aí tudo bem funciona com o find só que eu preciso quee pegue o link limpo algo como

img/imagem.gif

e não:

href="img/imagem.gif"
src="img/imagem.gif"
url(img/imagem.gif)

logo eu usaria uma variavel e daria um replace, é o que me vem a mente assim ficaria:

String texto2 = "url(\"primeiro url\")\n" +
    "url('2 url')\n" +
    "href=\"1 href\"\n" +
    "src=\"1 src\"\n" +
    "src='2 src'\n" +
    "url('3 url')\n" +
    "\n" +
    ".camera_target_content .camera_link {\n" +
    "   background: url(../images/blank.gif);\n" +
    "   display: block;\n" +
    "   height: 100%;\n" +
    "   text-decoration: none;\n" +
    "}";

    String exp = "(?:href|src)=[\"'](.+)[\"']+|(?:url)\\([\"']*(.*)[\"']*\\)";
    // expressão para pegar os links do src e do href
    Pattern pattern = Pattern.compile(exp);

    // preparando expressao
    Matcher matcher = pattern.matcher(texto2); 


    // pegando urls e guardando na lista
    while(matcher.find()) {


    String s = matcher.group(2);
    System.out.println(s);  


    }

Acontece que com o replace não dá certo! ele não pega a url perfeitamente, alguem pode me ajudar?

Criado 16 de novembro de 2013
Respostas 0
Participantes 1