<a  class='titleLength' href='showArticle.asp?id=2189' title='联系表' >联系表</a>
<a  class='titleLength' href='showDeclaration.asp?id=1375' title='通知' >通知' </a>
<a  class='titleLength' href='showNews.asp?id=4919' title='代表' >代表</a>
<a  target='_blank'  class='titleLength' href='showNews.asp?id=4918' title='工作会(图)' >工作会(图)</a> 
<a  class='titleLength' href='showDeclaration.asp?id=1735' title='关于' >关于</a> 这是要匹配的URL,我写的表达式:
<a\\s+.*class='titleLength'\\s+href=\\\'([a-z]*\\.asp\\?id=\\d+(\\&depart=\\d+)?\\\'>(.*?)<\\/a>经过测试不对请大家指点一下,谢谢!

解决方案 »

  1.   

    写一个URL,能够匹配上面5种形式的URL我想从一个网页文件中取出我需要的链接
      

  2.   

    js正则<a\s+(class='titleLength'\s+)?(target='[^']*'\s+)?(class='[^']*'\s+)?href='[^']*'\s+(title='[^']*')\s+>[^/]*/a>
      

  3.   

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class RegexTest {
    public static void main(String[] args) {
    String str="<a  class='titleLength' href='showArticle.asp?id=2189' title='联系表' >联系表 </a>" +
    "<a  class='titleLength' href='showDeclaration.asp?id=1375' title='通知' >通知' </a>" +
    "<a  class='titleLength' href='showNews.asp?id=4919' title='代表' >代表 </a>" +
    "<a  target='_blank'  class='titleLength' href='showNews.asp?id=4918' title='工作会(图)' >工作会(图) </a>" +
    "<a  class='titleLength' href='showDeclaration.asp?id=1735' title='关于' >关于 </a> ";
    String regex="<a\\s*(target='.*')?\\s*class='titleLength'\\s*href='\\w*\\.asp\\?id=\\d{4}'\\s*title='[\u4E00-\u9FA5]*?(([\u4E00-\u9FA5]))?'\\s*>[\u4E00-\u9FA5]*?(([\u4E00-\u9FA5]))?'?\\s*</a>";
    Pattern p=Pattern.compile(regex);
    Matcher m=p.matcher(str);
    while(m.find()){
    System.out.println(m.group());
    }
    }}
    测试结果:<a  class='titleLength' href='showArticle.asp?id=2189' title='联系表' >联系表 </a>
    <a  class='titleLength' href='showDeclaration.asp?id=1375' title='通知' >通知' </a>
    <a  class='titleLength' href='showNews.asp?id=4919' title='代表' >代表 </a>
    <a  target='_blank'  class='titleLength' href='showNews.asp?id=4918' title='工作会(图)' >工作会(图) </a>
    <a  class='titleLength' href='showDeclaration.asp?id=1735' title='关于' >关于 </a>
    正则里面(图)的括号,是全角下输入的括号
      

  4.   

    重新贴一下正则,正则太长,贴代码时竟然一行显示不全
    String regex="<a\\s*(target='.*')?\\s*class='titleLength'\\s*href='\\w*\\.asp\\?id=\\d{4}'\\s*title='[\u4E00-\u9FA5]*?(([\u4E00-\u9FA5]))?'\\s*>" +
    "[\u4E00-\u9FA5]*?(([\u4E00-\u9FA5]))?'?\\s*</a>";
    这个正则匹配你给的那5个是没问题,是根据那5个的格式来的
      

  5.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class ParseHtml { public static void main(String[] args) {
    String html = "<a  class='titleLength' href='showArticle.asp?id=2189' title='联系表' >联系表 </a>"+ 
    "<a  class='titleLength' href='showDeclaration.asp?id=1375' title='通知' >通知' </a> "+
    "<a  class='titleLength' href='showNews.asp?id=4919' title='代表' >代表 </a> "+
    "<a  target='_blank'  class='titleLength' href='showNews.asp?id=4918&depart=542' title='工作会(图)' >工作会(图) </a> "+
    "<a  class='titleLength' href='showDeclaration.asp?id=1735' title='关于' >关于 </a> "+
    "<a href='showdeclaration.asp?sid=5424'>位置</a>";
    String pattern = "(?<=href=\\')(\\w+?\\.asp\\?id=\\d+(\\&depart=\\d+)?)(?=\\')";
    Matcher matcher = Pattern.compile(pattern).matcher(html);
    while(matcher.find()){
    System.out.println(matcher.group());
    }
    }}