【问题标题】:Split an array based on regex根据正则表达式拆分数组
【发布时间】:2021-06-04 10:48:08
【问题描述】:

抱歉,这是一个愚蠢的问题,因为我是 Java 新手。

如果我有一个类似的数组

{ "A123","blue","A456","red","green",
  "B111","purple","C444","blue","green","yellow","pink" }

如何将其拆分为较小的数组,其中第一个元素应该是一个字母,后跟 3 个数字?例如,输出将是:

{ {"A123","blue"},{"A456","red","green"},
  {"B111","purple"},{"C444","blue","green","yellow","pink"} }

模式[A-Z][0-9]{3} 后面可以有任意数量的元素,直到模式下一次出现。

【问题讨论】:

    标签: java arrays regex split


    【解决方案1】:

    这是一个例子:

    @Test
    public void testArr() {
        String[] strings = new String[]{
                "A123", "blue", "A456", "red", "green",
                "B111", "purple", "C444", "blue", "green", "yellow", "pink"
        };
    
        List<List<String>> output = new ArrayList<>();
        List<String> currentList = null;
        for (String s : strings) {
            if (s.matches("[A-Z][0-9]{3}")) {
                // start new sub-list
                currentList = new ArrayList<>();
                output.add(currentList);
            }
            currentList.add(s);
        }
    
        System.out.println(output);
    }
    

    它产生:

    [[A123, blue], [A456, red, green], [B111, purple], [C444, blue, green, yellow, pink]]
    

    【讨论】:

      【解决方案2】:

      您可以从此数组中收集地图:

      String[] arr = {"A123", "blue", "A456", "red", "green",
              "B111", "purple", "C444", "blue", "green", "yellow", "pink"};
      
      Map<String, List<String>> map = new LinkedHashMap<>();
      
      // assume that first is the 'primary' element
      List<String> list = null;
      for (String str : arr) {
          // if this is a 'primary' element
          // regex:
          // \\D{1} - first character is non-digit
          // \\d+   - non-empty sequence of digits
          if (str.matches("\\D{1}\\d+")) {
              // put new entry into the map and initialise new list
              list = map.computeIfAbsent(str, el -> new ArrayList<>());
          } else {
              // otherwise, add the 'secondary' element to the list
              list.add(str);
          }
      }
      
      // output
      map.forEach((k, v) -> System.out.println(k + "=" + v));
      //A123=[blue]
      //A456=[red, green]
      //B111=[purple]
      //C444=[blue, green, yellow, pink]
      

      // convert a map to a 2d array, if needed
      String[][] arr2d = map.entrySet().stream()
              .map(entry -> Stream
                      .concat(Stream.of(entry.getKey()), entry.getValue().stream())
                      .toArray(String[]::new))
              .toArray(String[][]::new);
      
      // output
      Arrays.stream(arr2d).map(Arrays::toString).forEach(System.out::println);
      //[A123, blue]
      //[A456, red, green]
      //[B111, purple]
      //[C444, blue, green, yellow, pink]
      

      【讨论】:

        猜你喜欢
        • 2012-06-14
        • 2019-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多