String s = "99100060000_TXN_CSTM_INFO_0000_20130719_I_0001_0001.xml.err";
如何分隔出来
99100060000
TXN_CSTM_INFO
0000
20130719
I
0001
0001
“TXN_CSTM_INFO”为变长字符串 其他都为定长字符串
正则表达式String

解决方案 »

  1.   

    public static void main(String[] args) throws IOException {
    String s = "99100060000_TXN_CSTM_INFO_0000_20130719_I_0001_0001.xml.err";
    Pattern pat = Pattern.compile("(_?\\d+_?|[_a-zA-Z]+)");
    Matcher mat = pat.matcher(s);
    while (mat.find()) {
    String str = mat.group();
    int i = str.lastIndexOf("_");
    if (i != -1) {
    System.out.println(str.substring(0, i));
    } else {
    System.out.println(str); }
    }
    }
      

  2.   

    我能取出指定的值吗 比如这个TXN_CSTM_INFO
      

  3.   

    String s = "99100060000_TXN_CSTM_INFO_0000_20130719_I_0001_0001.xml.err";
    // Pattern pat = Pattern.compile("(_?\\d+_?|[_a-zA-Z]+)");
    Pattern pat = Pattern
    .compile("99100060000_(.*?)_0000_20130719_I_0001_0001.xml.err");
    Matcher mat = pat.matcher(s);
    while (mat.find()) {
    String str = mat.group(1);
    System.out.println(str);
    }
      

  4.   

    说实话你这个字符串拼接的不科学。除了你需要的东西有段字符串之外后面还有个I所以比较肯爹。如果能保证需要的TXN_CSTM_INFO这个排在前面的话可以把while改成if,或者在while循环里面做处理public static void main(String[] args) {
    String s = "99100060000_TXN_CSTM_INFO_0000_20130719_I_0001_0001.xml.err";
    Matcher m = Pattern.compile("\\d_([a-zA-Z_]+?)_\\d").matcher(s);
    while(m.find()){
    System.out.println(m.group(1));
    }
    }