void go() {
 String s="15~4, 10~1";
String m="(\\[\\d\\]*?)~";
 Pattern pmp3 = Pattern.compile(m);
Matcher mmp3 = pmp3.matcher(s);
System.out.println(mmp3.find());
if (mmp3.find()) {
System.out.println("文章正文" + mmp3.group(1));
 
} }
上面是程序,我想得到“~”前面的数字,为什么不对啊,请指教 

解决方案 »

  1.   

    void go() {
     String s="15~4, 10~1";
    String m="^(\\d+)";//改下这
     Pattern pmp3 = Pattern.compile(m);
    Matcher mmp3 = pmp3.matcher(s);
    //System.out.println(mmp3.find());这句不要
    if (mmp3.find()) {
    System.out.println("文章正文" + mmp3.group(1));
     }
    }
      

  2.   

    这样改下,问题解决了!
     String s="15~4, 10~1";
    String m="(\\d+)~";//改下这
     Pattern pmp3 = Pattern.compile(m);
    Matcher mmp3 = pmp3.matcher(s);
      //System.out.println(mmp3.find());//这句不要
    while(mmp3.find()) {
    System.out.println("文章正文" + mmp3.group(1));
     }