现有个字符串 
0001#10#11#0001#20#41#0002#500#565#0003#2400#2402#0004#2000#2100#2103#0004#2000#2200#2206#0005#500#501#0006#A0000#A0400#A0401#0006#F0000#F0600#F0609#0006#V0000#V9900#0007#A0000#A0400#A0401#0007#A0000#A1700#A1706#0008#100#102要用split 拆分一个数组
元素是 a[0]='0001#10#11#' a[1]='0001#20#41#' 以0001 0002拆分最大是0010
这个正则怎么写呢 ?

解决方案 »

  1.   

    String str = "0001#10#11#0001#20#41#0002#500#565#0003#2400#2402#0004#2000#2100#2103#0004#2000#2200#2206#0005#500#501#0006#A0000#A0400#A0401#0006#F0000#F0600#F0609#0006#V0000#V9900#0007#A0000#A0400#A0401#0007#A0000#A1700#A1706#0008#100#102#0010#4#0019#3#";
    String[] strs = str.split("(?<=#)(?=(000[0-9])|(0010))");
    for(String s : strs) {
      System.out.println(s);
    }
      

  2.   

    呀~~写错了,是从0001开始的,改一下:public static void main(String[] args) throws Exception {
      String str = "0001#10#11#" +
          "0001#20#41#" +
          "0002#500#565#" +
          "0003#2400#2402#" +
          "0004#2000#2100#2103#" +
          "0004#2000#2200#2206#" +
          "0005#500#501#" +
          "0006#A0000#A0400#A0401#" +
          "0006#F0000#F0600#F0609#" +
          "0006#V0000#V9900#" +
          "0007#A0000#A0400#A0401#" +
          "0007#A0000#A1700#A1706#" +
          "0008#100#102#" +
          "0010#4#0019#3#";
      String[] strs = str.split("(?<=#)(?=(000[1-9]#)|(0010#))");
      for(String s : strs) {
        System.out.println(s);
      }
    }
      

  3.   

    把 0改成1就可以了
    hehe  刚才我还奇怪怎么结不帖子原来正好你回复了
      

  4.   

    哦对了 比如你写的这个例子
     String str = "0001#10#11#" +
          "0001#20#41#" +
          "0002#500#565#" +
          "0003#2400#2402#" +
          "0004#2000#2100#2103#" +
          "0004#2000#2200#2206#" +
          "0005#500#501#" +
          "0006#A0000#A0400#A0401#" +
          "0006#F0000#F0600#F0609#" +
          "0006#V0000#V9900#" +
          "0007#A0000#A0400#A0401#" +
          "0007#A0000#A1700#A1706#" +
          "0008#100#102#" +
          "0010#4#0019#3#";如果我向 出来的是
                "0001#11#" +
          "0001#41#" +
          "0002#565#" + 这样的就是 跟着0001的第一个数字不要 可以写正则吗?
      

  5.   

    这样只能先拆开,再将其替换掉。public static void main(String[] args) throws Exception {
      String str = "0001#10#11#" +
          "0001#20#41#" +
          "0002#500#565#" +
          "0003#2400#2402#" +
          "0004#2000#2100#2103#" +
          "0004#2000#2200#2206#" +
          "0005#500#501#" +
          "0006#A0000#A0400#A0401#" +
          "0006#F0000#F0600#F0609#" +
          "0006#V0000#V9900#" +
          "0007#A0000#A0400#A0401#" +
          "0007#A0000#A1700#A1706#" +
          "0008#100#102#" +
          "00019#4#0019#3#";
      String[] strs = str.split("(?<=#)(?=(000[1-9]#)|(0010)#)");    
          
      for(int i=0; i<strs.length; i++) {
        // 去掉第一组中的数字
        strs[i] = strs[i].replaceAll("(\\d{4})(#)(\\d*)(#)", "$1$4");
        // 去掉第一组中的数字或外文
        //strs[i] = strs[i].replaceAll("(\\d{4})(#)(\\w*)(#)", "$1$4");
      }
      
      for(String s : strs) {
        System.out.println(s);      
      }
    }需要粘贴的话,要把两个全身空格替换成Tab或去掉,否则编译会出错。
      

  6.   

    hehe  这样基本就可以满足了
      

  7.   

    replaceAll("(\\d{4})(#)(\\d*)(#)", "$1$4");  有点问题如果"0007#A0000#A1700#A1706#" 这样的会替换成'0007#A1706#" '多替换了1个 A1700
      

  8.   

    没有啊,使用 \\d* 的话,“0007#A0000#A1700#A1706#”根本不会替换的啊。你可能使用了“\\w*”,但是也不会变成“0007#A1706#”,而是变成“0007#A1700#”,你把 replaceAll 中的正则表达式最前面加上“^”试试看。