拆分列表中的右子字符串

时间:2015-06-07 12:25:30

标签: java regex

我正在尝试将行字符串存储在列表中以进行处理。在当前状态下,第一个元素被删除。我想在处理之前从行字符串中删除字母子字符串。我该如何解决这个问题?

我感谢任何帮助。

简单:

stop 04:48 05:18 05:46 06:16 06:46 07:16 07:46 16:46 17:16 17:46 18:16 18:46 19:16
Apple chair car 04:52 05:22 05:50 06:20 06:50 07:20 07:50 16:50 17:20 17:50 18:20 18:50 19:20

结果:

   [04:48, 05:18, 05:46, 06:16, 06:46, 07:16, 07:46, 16:46, 17:16, 17:46, 18:16, 18:46, 19:16]
   [04:52, 05:22, 05:50, 06:20, 06:50, 07:20, 07:50, 16:50, 17:20, 17:50, 18:20, 18:50, 19:20]

代码:

if (line.contains(":")) {
            String delims = " ";
            String[] tokens = line.split(delims);
            List<String> list = new ArrayList<String>(
                    Arrays.asList(tokens));
            list.remove(0);
            System.out.println(tokens);

        } 

2 个答案:

答案 0 :(得分:4)

首先替换然后分裂。

string.replaceFirst("(?m)^.*?(?=\\d+:\\d+)", "").split("\\s+");

DEMO

  • string.replaceFirst("(?m)^.*?(?=\\d+:\\d+)", "")将使用空字符串替换起始字母和空格。

  • 现在,对结果字符串进行空格分割将为您提供所需的输出。

答案 1 :(得分:0)

这是一个没有正则表达式的替代方案,最终结果将是您可以按空格分割的字符串。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:group name="ConversationObjectGroup">
    <xs:choice>
      <xs:element name="Message"/>
      <xs:element name="End"/>
      <xs:element name="Effect"/>
    </xs:choice>
  </xs:group>

  <xs:element name="yesOrNoAnswer" type="YesOrNoAnswerType"/>

  <xs:complexType name="YesOrNoAnswerType" mixed="true">
    <xs:sequence>
      <xs:element name="yes" type="yesOrNoType"/>
      <xs:element name="no" type="yesOrNoType"/>
    </xs:sequence>
    <xs:attribute name="actor"/>
    <xs:attribute name="bar"/>
    <xs:attribute name="points"/>
    <xs:attribute name="percentage"/>
  </xs:complexType>

  <xs:complexType name="yesOrNoType" mixed="true">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:group ref="ConversationObjectGroup"/>
      <xs:element ref="yesOrNoAnswer"/>
    </xs:choice>
  </xs:complexType>

</xs:schema>

结果&gt;&gt; 04:52 05:22 05:50 06:20 06:50 07:20 07:50 16:50 17:20 17:50 18:20 18:50 19:20