【问题标题】:How to Split text by Numbers and Group of words如何按数字和单词组拆分文本
【发布时间】:2019-06-14 07:30:45
【问题描述】:

假设我有一个包含 - 一些逗号分隔的字符串 - 和文字

  my_string =  "2 Marine Cargo       14,642 10,528       16,016 more text 8,609 argA 2,106 argB"

我想将它们提取到一个由“数字”和“单词组”分割的数组中

 resultArray = {"2", "Marine Cargo", "14,642", "10,528", "16,016",
                "more text", "8,609", "argA", "2,106", "argB"};

注意0:每个条目之间可能有多个空格,应该忽略。

注 1:“Marine Cargo”和“more text”没有分成不同的字符串,因为它们是一组没有数字分隔的单词。 而 argA 和 argB 是分开的,因为它们之间有一个数字。

【问题讨论】:

  • 2 Marine Cargo,我们怎么知道Cargo属于Marine
  • 您的预期输出中的值“14,642”是单个值还是两个值,例如:“14”和“642”?
  • 没有足够的信息来真正解决这个问题。我们怎么知道Marine Cargo 应该是一个元素?它不应该与2 一起导致2 Marine Cargo 吗?然后我们可以假设每个元素的长度为 20-25 个字符并添加一些填充。
  • @StephanHogenboom ya 14,642 是单个值
  • 我已经改写了你的问题和标题。您之前的标题太笼统,问题也太含糊(导致一波否决票)。希望对您有所帮助。

标签: java regex


【解决方案1】:

您可以尝试使用此正则表达式进行拆分

([\d,]+|[a-zA-Z]+ *[a-zA-Z]*) //note the spacing between + and *.
  • [0-9,]+ // 将搜索一个或多个数字和逗号
  • [a-zA-Z]+ [a-zA-Z] // 将搜索一个单词,后跟一个空格(如果有),然后是另一个单词(如果有)。

    String regEx = "[0-9,]+|[a-zA-Z]+ *[a-zA-Z]*";
    

你像这样使用它们

public static void main(String args[]) {

  String input = new String("2 Marine Cargo       14,642 10,528       16,016 more text 8,609 argA 2,106 argB");
  System.out.println("Return Value :" );      

  Pattern pattern = Pattern.compile("[0-9,]+|[a-zA-Z]+ *[a-zA-Z]*");

  ArrayList<String> result = new ArrayList<String>();
  Matcher m = pattern.matcher(input);
  while (m.find()) { 
         System.out.println(">"+m.group(0)+"<");  
         result.add(m.group(0));

   }
}

以下是从https://regex101.com自动生成的正则表达式的输出以及详细说明

1st Alternative [0-9,]+
Match a single character present in the list below [0-9,]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
, matches the character , literally (case sensitive)


2nd Alternative [a-zA-Z]+ *[a-zA-Z]*
Match a single character present in the list below [a-zA-Z]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)
 * matches the character   literally (case sensitive)
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [a-zA-Z]*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
a-z a single character in the range between a (index 97) and z (index 122) (case sensitive)
A-Z a single character in the range between A (index 65) and Z (index 90) (case sensitive)

【讨论】:

    【解决方案2】:

    如果空格是您的问题。 String#split 将正则表达式作为参数。然后你可以这样做: my_list = Arrays.asList(my_string.split("\s?"));

    但是,这并不能解决所有问题,就像 cmets 中提到的那样。

    【讨论】:

      【解决方案3】:

      你可以这样做:

          List<String> strings = new ArrayList<>();
          String prev = null;
          for (String w: my_string.split("\\s+")) {
              if (w.matches("\\d+(?:,\\d+)?")) {
                  if (prev != null) {
                      strings.add(prev);
                      prev = null;
                  }
                  strings.add(w);
              } else if (prev == null) {
                  prev = w;
              } else {
                  prev += " " + w;
              }
          }
          if (prev != null) {
              strings.add(prev);
          }
      

      【讨论】:

        【解决方案4】:

        我喜欢Angel Koh solution 并想添加它。只有当数字部分由一或两个部分组成时,他的解决方案才会匹配。

        如果您还想捕获由三个或更多部分组成的部分,则必须将正则表达式更改为:([\d,]+|[a-zA-Z]+(?: *[a-zA-Z])*)
        如果需要,非捕获组(?: *[a-zA-Z]) 会无限重复,并将捕获所有纯数字部分。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-13
          相关资源
          最近更新 更多