gpt4 book ai didi

java - 无法解析变量。安卓正则表达式

转载 作者:行者123 更新时间:2023-11-29 22:18:21 24 4
gpt4 key购买 nike

我正在尝试创建一个使用正则表达式从网站抓取数据然后显示的程序。

我已经尝试了一段时间来弄清楚发生了什么,但我做不到。

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);
}
}
}

最佳答案

Matcher 实例的范围在 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);
}
}
...

关于java - 无法解析变量。安卓正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7953520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com