-1

I've tryied convert a snippet code from C# to Java but without success. My doubt is in Regex classes.

C# code

string estado = Regex.Match(pagina, "<td width=\"25\" style=\"padding: 2px\">(.*)</td>").Groups[1].Value;
estado = "<label>" + estado + "</label>";

In this code, I declare a string called estado and search in a another string called pagina a specific character.

How can I do this Regex in Java ?

HTML (I simplify the sample because is too big, so I put three dots where don't means so much to me)

<!DOCTYPE html SYSTEM "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Correios</title>
...
<td width="268" style="padding: 2px">Rua Satelite</td> 
<td width="140" style="padding: 2px">Caicara</td> 
<td width="140" style="padding: 2px">Belo Horizonte</td> 
<td width="25" style="padding: 2px">MG</td> 
<td width="65" style="padding: 2px">30280-291</td> 
</tr></table>
...

I need these <td> tags, but in Java Code.

Lucas_Santos
  • 4,638
  • 17
  • 71
  • 118

1 Answers1

1

Java does have regex. The regex capabilities are provided by the classes Pattern and Matcher. Your code would become:

String estado = Pattern.compile("<td width=\"25\" style=\"padding: 2px\">(.*)</td>").matcher(pagina).group();
estado = "<label>" + estado + "</label>";

For more info, see the package summary for java.util.regex.

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
tbodt
  • 16,609
  • 6
  • 58
  • 83
  • Without success, I got the `Exception: java.lang.IllegalStateException: No match found Message: java.lang.IllegalStateException: No match found` but I update my question where we can see that exist the `` that I want – Lucas_Santos Oct 14 '13 at 00:18