quarta-feira, 3 de outubro de 2012

Recuperar o conteúdo do uma tag HTML com regex

Site Teste
http://regexpal.com/
http://www.regexplanet.com/advanced/java/index.html

Regex
<tag.*<\/tag>
ou
<tag>(?:[^\n]*(\n+|.*))+</tag>
ou
<\s*tag[^>]*>(.*?)<\s*/\s*tag>
ou
(?<=<\s*tag[^>]*>)(.*?)(?=<\s*/\s*tag\s*>)

String de teste

<root>
<tag>valor</tag>
</root>


(?<=% of )(.*)(?= at )



https://www.regexpal.com/96872
https://www.regexpal.com/27540


regex no java


String html="<root><tag>Medicamento</tag></root>"
String conteudo = null;
Pattern pattern = Pattern.compile("<tag.*<\\/tag>");
Matcher matcher = pattern.matcher(html);
if (matcher.find()) {
    conteudo = matcher.group(0).replace("<tag>", "").replace("</tag>", "").toUpperCase();
}


Nenhum comentário:

Postar um comentário