public List<String> getTag(String content,String tag) throws Exception{
try{
Pattern pattern=Pattern.compile(tag);
Matcher matcher=pattern.matcher(content);
List<String> list=new ArrayList<String>();

while (matcher.find()){
   list.add(matcher.group());
}

return list;
}catch(Exception e){
throw e ;
}
}
我用matcher.find()方法匹配数据,但只能取到匹配到得数据,而那些不匹配的数据又需要当做坏数据记录下来~~该怎么办呢????在线求解答!!!!

解决方案 »

  1.   

    看具体需求了
    简单点replaceAll所有匹配的数据
    剩下的就是不匹配的数据了
      

  2.   

    public List<String> getTag(String content,String tag) throws Exception{
            try{
                Pattern pattern=Pattern.compile(tag);
                Matcher matcher=pattern.matcher(content);
                List<String> list=new ArrayList<String>();
                
                while (matcher.find()){
                   list.add(content.subString(matcher.end()));
            }
            
                return list;
            }catch(Exception e){
                throw e ;
            }
        }
      

  3.   

    假设要找数字,有两个样本
    a1234x5678
    abcdxyz你的不匹配部分是指
    1 abcdxyz,第一个样本因为包含数字而没问题
    2 第一个里面的a,x以及第二个abcdxyz如果是1
    if (matcher.find()) {
      do {
      } while (matcher.find());
    } else {
    }如果是2,用split方法,把原来的匹配部分当作分隔符