如字符串:I'm from China,I am-# a comp.uter-# enthu.siast-#得到一个数组,里边保存数据如下:am,uter,siast
提示,字符串规则,提取前面是空格或者原点 至 -# 之间的字符请问正则如何写???

解决方案 »

  1.   

    你的规则有问题,如果前面是空格,那from China为什么不提出来呢?
      

  2.   


    public static void main(String[] args) {
    String content = "I'm from China,I am-# a comp.uter-# enthu.siast-#";
    Pattern pattern = Pattern.compile("[^ |.]+(?=-#)");
    Matcher matcher = pattern.matcher(content);
    while(matcher.find()){
    System.out.println(matcher.group());
    }
    }
      

  3.   

    非常感谢 wapigzhu ,如果是这种的呢?
    China,I :am a :comp uter enthu :siast"如何书写提取 am comp siast 的正则表达式
      

  4.   

    不知道你具体需求是什么..也不知道你是哪种方式用的这个正则
    不过\w 单词字符:[a-zA-Z_0-9]
    也就是说,比如是 China,I :am a :comp*uter enthu :siast
    那提取出来也是 :am :comp :siast ,
    而不是:am :comp*uter  :siast
    不知道是不是你想要的结果
    另外(?::)+\\w*这个效果和:+\w*不是一样的吗
      

  5.   

    有一种情况不提取,类似":hello"  ':hello'这种值提取 :hello 这种,上面代码好像实现不了
      

  6.   

    你是说':hello'这种不用提取出来是吧?String content ="China,I :am a :comp uter enthu :siast ':hello'";
    Pattern p = Pattern.compile("(?<=[^']:)\\w+");
    Matcher m = p.matcher(content);
    while(m.find()){
    System.out.println(m.group());
    }