大家好 我想问个关于正则式的问题  下面是原代码,想从当中提出某些内容:
<div id="main">
<h2>Madonna</h2>
<dl>
<dt>Website /dt>
<dd>
                     <a href="http://www.madonna.com/"  title="Go to the Madonna website">www.madonna.com </a>
</dd>
<dt id="artPhoto">Artist photo</dt>
<dd id="photo"><img src="images/madonna.jpg" alt="Madonna"  title="Madonna"/></dd>想提取当中 id=main的 "Madonna", "www.madonna.com "   和id="photo"的  madonna.jpg这张图片 请问用正则式的话  那表达式应该是怎样的? 请大家帮帮忙,因为刚第1天学正则式,所以比较迷茫, 如果大家关于正则式的好网站的话,麻烦也提供下给小弟多学习学习  谢谢

解决方案 »

  1.   

        String str = "<div id=\"main\">\n<h2>Madonna </h2>\n<dl>\n<dt>Website /dt>\n<dd>\n <a href=\"http://www.madonna.com/\"  title=\"Go to the Madonna website\">www.madonna.com  </a>\n</dd>\n<dt id=\"artPhoto\">Artist photo </dt>\n<dd id=\"photo\"> <img src=\"images/madonna.jpg\" alt=\"Madonna\"  title=\"Madonna\"/> </dd> ";
        Pattern p = Pattern.compile("<div id=\"main\">.*?<a href=\"http://(.*?)[/]?\".*?<img src=\"images/(.*?)\"", Pattern.DOTALL);
        Matcher m = p.matcher(str);
        while (m.find()) {
          System.out.println(m.group(1));
          System.out.println(m.group(2));
        }
      

  2.   


    //竹子露了一个,
    public static void main(String[] args) throws IOException, InterruptedException {
            String s = "<div id=\"main\">\n<h2>Madonna </h2>\n<dl>\n<dt>Website /dt>\n<dd>\n <a href=\"http://www.madonna.com/\"  title=\"Go to the Madonna website\">www.madonna.com  </a>\n</dd>\n<dt id=\"artPhoto\">Artist photo </dt>\n<dd id=\"photo\"> <img src=\"images/madonna.jpg\" alt=\"Madonna\"  title=\"Madonna\"/> </dd> ";
            Pattern p = Pattern.compile(
                "<div id=\"main\">\n<h2>(.*?)</h2>.*?<a href=\"http://(.*?)[/]?\".*?<img src=\"images/(.*?)\"",
                Pattern.DOTALL);
            Matcher m = p.matcher(s);
            while (m.find()) {
                System.out.println(m.group(1));
                System.out.println(m.group(2));
                System.out.println(m.group(3));
            }
        }
      

  3.   

    你们好,我之前给的原代码只是整个原代码的其中一部分,当我用你们给的正则式去整个原代码中提取内容的时候,所提取出来的是空白,是因为原代码变的时候,正则式也要改变吗? 还是我的程序有问题呢? 现在我把程序和整个原代码都附加上来, 麻烦两位高手帮忙看写 谢谢
    程序如下: 
    public class SimpleAccess {  public static void main(String[] args) throws Exception{
    URL myURL = new URL("http://nebula.dcs.shef.ac.uk:8087/com4280/artists/artistd708.html?id=3");
    BufferedReader in = 
    new BufferedReader(
    new InputStreamReader(myURL.openStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
                        {
    String s=inputLine;
         Pattern p = Pattern.compile(
           "<div id=\"main\">\n\n<h2>(.*?)</h2>.*?<a href=\"http://(.*?)[/]?\".*?<img src=\"images/(.*?)\"",
           Pattern.DOTALL);
           Matcher m = p.matcher(s);
           while (m.find()) {
           System.out.println(m.group(1));
           System.out.println(m.group(2));
           System.out.println(m.group(3));
                    }
                       }
    in.close(); 
    }
    }
      

  4.   

    inputLine = in.readLine()) != null你不要每次匹配一行,而要按照我的建议,先把整个页面内容拿下来,再匹配。 OK!
      

  5.   

    以下是从该URL当中提取出来的原代码, 由于该网是属于学校内部网,外人无法访问,所以在此提供相关部分原代码  ,还有这个原代码很长,我们要取的是id=xxx 那张图片, 也就是后面还有很多张图片,所以是否能判断这个ID来取想去的那些图片? 这样的正则式应该是怎样的呢?
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
    <head>
    <title>Music Warehouse &raquo; Artist &raquo; </title>


    <link type="text/css" rel="stylesheet" href="css/style3.css" />
        </head><body id="artist"><div id="contentlayer">
    <div id="header">
    <h1><a href="index.html">The Music Warehouse</a></h1>
    <p id="byline">All your favourite artists and albums in one place</p>
    </div>

    <div id="main"> <h2>Madonna</h2>

    <dl>
    <dt>Website:</dt>
    <dd>
    <a href="http://www.madonna.com/" title="Go to the Madonna website">www.madonna.com 
    </a>
    </dd>

    <dt id="artPhoto">Artist photo</dt>
    <dd id="photo"><img src="images/madonna.jpg" alt="Madonna" title="Madonna"/></dd>