日期:2014-05-20  浏览次数:20865 次

我想用正则表达式读取Html文件中的URL该怎么实现?
我尝试了以下Scanner
看了说明发现Scanner好像只能读取用分隔符分隔的信息。
我想读取Html文件中由 <a   href= "URL "> XXXXXXXXX </a> 中的XXXX,该如何实现?只要用到正则表达式,实现方法不限。


------解决方案--------------------
第三方的包可以去apache官方网站去下载

package org.luyang.lang;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.Util;

public class RegularExpressions {
public static void main(String[] args) throws MalformedPatternException {


String str = " <a href=\ "http://localhost:8091/abc\ "> i love you </a> ";
String tag = " <a href=\ "(.*)\ "> ([^ </a> ]*) </a> ";
PatternCompiler compiler = new Perl5Compiler();
org.apache.oro.text.regex.Pattern patternTag = compiler.compile(tag,
Perl5Compiler.CASE_INSENSITIVE_MASK);
PatternMatcher matcher = new Perl5Matcher();
if (matcher.contains(str, patternTag)) {
MatchResult result = matcher.getMatch();
String url = result.group(1);
String content = result.group(2);
System.out.println(url);
System.out.println(content);
}
------解决方案--------------------
匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*