问题如下:
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.apache.oro.text.regex.MatchResult;String seesee = "fasfsda dfafds 6.56 TAB_Edison Column 6.58 ABC_Tomas Column";
// 搜索以数字+.+数字+空格+字母+_+空格 的字符串
String regex = "\\d+\\.\\d+\\s{1}\\w+\\_\\w+\\s{1}";     try{
       Pattern pattern = compiler.compile(regex);
       PatternMatcher matcher = new Perl5Matcher();
       if(matcher.contains(seesee,pattern)){
         MatchResult result = matcher.getMatch();
         System.out.println(result);
       }
     }
     catch(Exception e){
       e.printStackTrace();
     }照道理应该打印出6.56 TAB_Edison Column 6.58 ABC_Tomas Column,但现在只能打印出6.56 TAB_Edison Column ,如果把6.56 TAB_Edison Column 从String seesee中去掉,6.58 ABC_Tomas Column就能打印出来,这是为什么?我怎么才能把两个同时打印出来?求高手解答。

解决方案 »

  1.   

    String regex="\\d+\\.\\d+\\s+\\w+\\s+";\\w本身包含_ 空格也止一个
      

  2.   

    你的方法不行,我把完整代码贴一下import org.apache.oro.text.regex.Pattern; 
    import org.apache.oro.text.regex.PatternCompiler; 
    import org.apache.oro.text.regex.PatternMatcher; 
    import org.apache.oro.text.regex.Perl5Compiler; 
    import org.apache.oro.text.regex.Perl5Matcher; 
    import org.apache.oro.text.regex.MatchResult;   public static void main (String args[]) {
      String seesee = "fasfsda dfafds 6.56 TAB_Edison Column 6.58 ABC_Tomas Column "; 
      // 搜索以数字+.+数字+空格+字母+_+空格 的字符串 
      String regex = "\\d+\\.\\d+\\s{1}\\w+\\_\\w+\\s{1}"; 
              try{ 
                  PatternCompiler compiler = new Perl5Compiler();
                  Pattern pattern = compiler.compile(regex); 
                  PatternMatcher matcher = new Perl5Matcher(); 
                  if(matcher.contains(seesee,pattern)){ 
                      MatchResult result = matcher.getMatch(); 
                      System.out.println(result); 
                  } 
              } 
              catch(Exception e){ 
                  e.printStackTrace(); 
              } 
      }
      

  3.   

    我没看你的那种编译方式为什么不行 我贴上我的运行没问题的吧
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Test {
    public static void main(String[] args) throws java.text.ParseException {
    String regex = "\\d+\\.\\d+\\s+\\w+\\s+ ";
    String input= "fasfsda   dfafds   6.56   TAB_Edison   Column   6.58   ABC_Tomas   Column   "; Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(input);
    while(m.find()){
    System.out.println(m.group());
    }
    }
    }/*
    6.56   TAB_Edison   
    6.58   ABC_Tomas   
    */