请问这样的用正则怎么匹配??
谢谢大家帮助。。
[b]问题补充:[/b]
我是想要得到他的URL。。。
例如上面的的http://www.autohome.com.cn/780/
[code="java"]
import java.util.regex.*;
public class RegExpParseHTML {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String html = "<a href=\"http://www.autohome.com.cn/780/\" style=\"text-decoration:none;\" target=\"_blank\" title=\"[D] 大众UP频道\" >[D] 大众UP频道</a>";
String href = parseHref(html);
System.out.println(href);
}
public static String parseHref(String html)
{
String regex = "<a[\\s]+href[\\s]*=[\\s]*\"([^<\"]+)\"";
//String regex = "[^.]";
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(html);
StringBuffer ret = new StringBuffer();
while(m.find())
{
ret.append(m.group(1));
}
return ret.toString();
}
}
[/code]
<a[\s]+(href)i[\s]*=[\s]*
给你举个例子吧:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
[code="java"]public class Test {
public static void main(String[] args) {
String str = "<a href ='http://www.autohome.com.cn/780/' style='text-decoration:none;' target='_blank' title='[D] 大众UP频道' >[D] 大众UP频道</a>mmmm+"
+ "\njflsk;df fds fsdfsd asfd asfd jsdl;k <a href='http://www.autohome.com.cn/780/' style='text-decoration:none;'/a>wjal;kfjs;ld\n";
Pattern pattern = Pattern.compile("<a\\s+(href).*a>");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}[/code]
[color=red][size=large]注:str就是你要匹配的字符串,换为你自己的就行了。[/size][/color]
你如果只想要这个,其实用String里面提供的方法就可以呀。
[code="java"]public class AAA {
public static void main(String[] args) {
String str = "[D] 大众UP频道 ";
String[] strs = str.split("'");
System.out.println(strs[1]);
}
}[/code]
[size=medium]如果你只单单指的是这个字符串,那么就采用下边的,很简单:[/size]
[code="java"]public class AAA {
public static void main(String[] args) {
String str = String str = "[D] 大众UP频道";
String[] strs = str.split("\"");
System.out.println(strs[1]);
}
} [/code]
[size=medium]如果你想把整个文档的Http都找出来,那就用下边的:[/size]
[code="java"]import java.util.ArrayList;
import java.util.List;
public class AAA {
public static void main(String[] args) {
String str = "[D] 大众UP频道";
for (String httpStr : getURL(str))
System.out.println(httpStr);
}
public static List<String> getURL(String str) {
List<String> results = new ArrayList<String>();
String[] strs = str.split("\"");
for (String temp : strs) {
if (temp.startsWith("http"))
results.add(temp);
}
return results;
}
}[/code]