1

I am trying to create a program that uses regular expression to scrape data from a website and then display it.

I've been trying for a while to figure out what's going on, but i can't.

I am getting this error on those 3 lines: m cannot be resolved

while (m.find()) {
String stateURL = m.group(1);
String stateName = m.group(2);


public class VisualizaActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.visualiza);

    /* Imprime na tela
    TextView tv = new TextView(this);
    tv.setText(stateName + "," + stateURL);
    setContentView(tv);  */
    }



String expr = "<td><span\\s+class=\"flagicon\"[^>]*>";


public static CharSequence getURLContent(URL url) throws IOException {
    URLConnection conn = url.openConnection();
    String enconding  = conn.getContentEncoding();
    if (enconding == null){
        enconding = "ISO-8859-1";
    }
    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), enconding));
    StringBuilder sb = new StringBuilder(16384);
    try{
        String line;
        while ((line = br.readLine()) != null){
            sb.append(line);
            sb.append('\n');
        }
    } finally{
        br.close();
    }
    return sb;
}
public void x()
{
    Pattern patt = Pattern.compile(expr,Pattern.DOTALL | Pattern.UNIX_LINES);
    try
    {
        URL url = new URL("http://en.wikipedia.org/wiki/Mexico");
        Matcher m = patt.matcher(getURLContent(url));
    }
    finally
    {       
    }
    while (m.find()) {
        String stateURL = m.group(1);
        String stateName = m.group(2);
        System.out.println(stateName + "," + stateURL);
    }
}
}
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
arielsp
  • 43
  • 1
  • 6
  • In addition to your scope problem already solved by Prince John Wesley you should not try to match html with regular expressions. Have a look here: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – phlogratos Oct 31 '11 at 12:20
  • I think you'd have a much better time using something like XPath, though I'm not sure if there is an HTML Agility pack (or similar) for Java – Code Jockey Oct 31 '11 at 15:39

1 Answers1

2

Scope of the Matcher instance is within the try block.

    try
    {
        URL url = new URL("http://en.wikipedia.org/wiki/Mexico");
        Matcher m = patt.matcher(getURLContent(url));
        while (m.find()) {
           String stateURL = m.group(1);
           String stateName = m.group(2);
           System.out.println(stateName + "," + stateURL);
        }
    }
    ...
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94