有一段字符串是:
dsad<td class=\"yfnc_tabledata1\"><small>Zions Bancorporation</small></td>dsa<td class=\"yfnc_tabledata1\"><small>wangking</small></td>
我怎么能够在这段字符串里获得Zions Bancorporation,wangking这俩字符串?正则怎么写呢?

解决方案 »

  1.   

    dsad<td class=\"yfnc_tabledata1\"><small>Zions Bancorporation</small></td>dsa<td class=\"yfnc_tabledata1\"><small>wangking</small></td>
      

  2.   

    我写的测试程序是这样的,可得到的永远都是一个数据。就是获得最后的数据。不知道问题出在哪里。import java.util.regex.Matcher;
    import java.util.regex.Pattern;/**
     * @author wangking E-mail:[email protected]
     * @version 创建时间:2009-9-16 下午01:02:38
     * 类说明
     */
    public class TestA { public static void main(String[] args) { Pattern pattern = Pattern.compile("<td class=\"yfnc_tabledata1\"><small>([a-zA-Z]+)(</small></td>)$");
    Matcher matcher = pattern.matcher("dsad<td class=\"yfnc_tabledata1\"><small>Zions Bancorporation</small></td>dsa<td class=\"yfnc_tabledata1\"><small>wangking</small></td>");
    if(matcher.find()){
    System.out.println(matcher.group(1));
    }

    }

    }
      

  3.   


     public static void main(String[] s){
       Pattern p = Pattern.compile("<small>([a-zA-Z ]+)</small>");
       String str = "dsad <td class=\"yfnc_tabledata1\"> <small>Zions Bancorporation </small> " +
        "</td>dsa <td class=\"yfnc_tabledata1\"> <small>wangking </small> </td>";
      Matcher m =  p.matcher(str);
      while(m.find()){
      System.out.println(m.group(1));
      }
       }Zions Bancorporation 
    wangking 
      

  4.   

    import java.util.regex.*;public class Test1 {
        
        public static void main(String[] args) {
            String str = "dsad <td class=\"yfnc_tabledata1\"> <small>Zions Bancorporation </small> </td>dsa <td class=\"yfnc_tabledata1\"> <small>wangking </small> </td>";
            Pattern ptn = Pattern.compile("<small>([^<>]+)</small>");
            Matcher mt = ptn.matcher(str);
            
            while (mt.find()) {
                   String text = mt.group(1).trim();
                   System.out.println("found [" + text + "]");
            }
        }
        
    }
      

  5.   


    import java.util.regex.*;public class Test1 {
        
        public static void main(String[] args) {
            String str = "dsad <td class=\"yfnc_tabledata1\"> <small>Zions Bancorporation </small> </td>dsa <td class=\"yfnc_tabledata1\"> <small>wangking </small> </td>";
            Pattern ptn = Pattern.compile("<small>([^<>]+)</small>");
            Matcher mt = ptn.matcher(str);
            
            while (mt.find()) {
                   String text = mt.group(1).trim();
                   System.out.println("found [" + text + "]");
            }
        }
        
    }
      

  6.   

    但是我在这个网页上抓取还是出问题。
    大虾帮我看看,我想抓取Zions Bancorporation, ZIMMER HOLDINGS INCimport java.io.*;
    import java.net.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class GetIp{ public static void main(String args[]) throws MalformedURLException,IOException {

    getContent("http://finance.yahoo.com/q/cp?s=^GSPC&alpha=Z");

    }

        public static void getContent(String URLSTR) {
            try {
                URL url = new URL(URLSTR);
                BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
                String line = null;
                
                Pattern namePat = Pattern.compile("<small>([a-zA-Z ]+)</small>");            while ((line = br.readLine()) != null) {
                
    //                System.out.println(line);
                    Matcher m = namePat.matcher(line);
                    if (m.find()) {
                    
                     System.out.println(m.group(1));
                    }
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }