String url="http://www.baidu.com/s?wd=%CC%EC%BB%A8%B0%E5";
Pattern wd= Pattern.compile("[&|\\?]wd=(.+)[&|$]");
java.util.regex.Matcher m = P_WD.matcher(url);
if (m.find())
{
String keywords = m.group(1);
System.out.println(keywords+":"+java.net.URLDecoder.decode(keywords, "GBK"));
}
如果  url="http://www.baidu.com/s?wd=%CC%EC%BB%A8%B0%E5&ie=gb2312" 这是可以取出关键字的. 为什么 url="http://www.baidu.com/s?wd=%CC%EC%BB%A8%B0%E5";  这样就不行???哪位大侠帮看看??

解决方案 »

  1.   

    Pattern wd= Pattern.compile("(?<=\\?wd=)(.+)(&.+)?");
      

  2.   

    $只是边界符号,并不是一个真正的字符
    --------
    我也是这样理解的呀例: http://www.baidu.com/s?wd=%CC%EC%BB%A8%B0%E5%CC%EC%BB%A8%B0%E5 之后没有东西了吧, 这应该叫"一行的结尾"吧为什么取不出来
    %CC%EC%BB%A8%B0%E5呀?????????
      

  3.   

    这样先判断 url 中是否存在 ‘&’字符,如果有 则用的 正则判断如果不是 则换一个判断~! public String getBody(String args) {
    String f = "[&|\\?]wd=(.+)";
    if (args.indexOf("&") != -1)
    f = "[&|\\?]wd=(.+)[&]"; Pattern p = Pattern.compile(f);
    Matcher m = p.matcher(args);
    if (m.find())
    return m.group(1).toString();
    else
    return "";
    }
      

  4.   

    Pattern wd= Pattern.compile("[&|\\?]wd=(.+?)(&.*)?$");
      

  5.   

    可能$只能放在最后,表示串结束,放在 []就表示一个普通字符而已
    xx$跟xx[$]不一样,楼主试试就知道了
      

  6.   

    Pattern wd = Pattern.compile("[&?]wd=(.+)(&.*)?$");orkeywords = url.replaceAll(".*[&?]wd=([^&]*).*", "$1")
      

  7.   

    Pattern wd = Pattern.compile("[&?]wd=([^&]*)");