Java 在列表中拆分右子字符串

Java 在列表中拆分右子字符串,java,regex,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:

我正在尝试将行字符串存储在列表中以进行处理。在当前状态下,仅删除第一个元素。我想在处理前从行字符串中删除字母子字符串。我怎样才能解决这个问题

谢谢你的帮助

简单:

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);

        } 
结果:

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);

        } 
代码:

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);

        } 
if(line.contains(“:”){
字符串delims=“”;
String[]tokens=line.split(delims);
列表=新的ArrayList(
asList(令牌));
列表。删除(0);
System.out.println(代币);
} 

首先更换,然后进行拆分

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

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

  • 现在,根据结果字符串对空格进行拆分将得到所需的输出


首先更换,然后进行拆分

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

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

  • 现在,根据结果字符串对空格进行拆分将得到所需的输出


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

      public class StringReplace {
      
      
      public static void main(String[] args) {
      
          String output = replace("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");
      
          List<String> tokens = new ArrayList<>();
      
          Collections.addAll(tokens, output.split(" "));          
      }
      
      private static String replace(String input) {
      
          char[] chars = input.toCharArray();
      
          StringBuilder builder = new StringBuilder();
      
          for (char character: chars) {
      
              // test against ASCII range 0 to ':' and 'space'
              if ((int)character > 47 && (int)(character) < 59 || (int)character == 32)  {
      
                  builder.append(character);  
              }
          }
      
          return builder.toString().trim();
      
          }
      }
      
      公共类StringReplace{
      公共静态void main(字符串[]args){
      字符串输出=替换(“苹果椅车04:52 05:22 05:50 06:20 06:50 07:20 07:50 16:50 17:20 17:50 18:20 19:20”);
      List tokens=new ArrayList();
      Collections.addAll(令牌,output.split(“”));
      }
      私有静态字符串替换(字符串输入){
      char[]chars=input.toCharArray();
      StringBuilder=新的StringBuilder();
      for(字符:chars){
      //根据ASCII范围0到“:”和“空格”进行测试
      如果((int)字符>47&(int)(字符)<59 | |(int)字符==32){
      builder.append(字符);
      }
      }
      返回builder.toString().trim();
      }
      }
      

      结果>>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这里有一个没有正则表达式的替代方法,最终结果将是可以按空格分割的字符串

      public class StringReplace {
      
      
      public static void main(String[] args) {
      
          String output = replace("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");
      
          List<String> tokens = new ArrayList<>();
      
          Collections.addAll(tokens, output.split(" "));          
      }
      
      private static String replace(String input) {
      
          char[] chars = input.toCharArray();
      
          StringBuilder builder = new StringBuilder();
      
          for (char character: chars) {
      
              // test against ASCII range 0 to ':' and 'space'
              if ((int)character > 47 && (int)(character) < 59 || (int)character == 32)  {
      
                  builder.append(character);  
              }
          }
      
          return builder.toString().trim();
      
          }
      }
      
      公共类StringReplace{
      公共静态void main(字符串[]args){
      字符串输出=替换(“苹果椅车04:52 05:22 05:50 06:20 06:50 07:20 07:50 16:50 17:20 17:50 18:20 19:20”);
      List tokens=new ArrayList();
      Collections.addAll(令牌,output.split(“”));
      }
      私有静态字符串替换(字符串输入){
      char[]chars=input.toCharArray();
      StringBuilder=新的StringBuilder();
      for(字符:chars){
      //根据ASCII范围0到“:”和“空格”进行测试
      如果((int)字符>47&(int)(字符)<59 | |(int)字符==32){
      builder.append(字符);
      }
      }
      返回builder.toString().trim();
      }
      }
      

      结果>>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

      我收到此错误
      类型字符串中的方法replaceFirst(String,String)不适用于参数(String)
      ?@MrAsker您不应该尝试编辑OP post来描述您的问题。相反,在搜索其他问题后再单独提问,这样将无法找到解决方案。如果在此编辑中添加代码,则问题是字符串是不可变的,因此
      行。replaceFirst
      不会更改
      列表中的字符串,而是返回带有替换值的新字符串。我收到此错误
      类型字符串中的方法replaceFirst(字符串,字符串)不适用于参数(字符串)
      ?@MrAsker您不应该试图编辑OP文章来描述您的问题。相反,在搜索其他问题后再单独提问,这样将无法找到解决方案。如果在此编辑中添加了代码,则问题是字符串是不可变的,因此
      行。replaceFirst
      不会更改
      列表中的字符串,而是返回带有替换值的新字符串。我已尝试过您的解决方案,但当字符为
      /
      时,我会得到此错误
      java.lang.StringIndexOutOfBoundsException:字符串索引输出范围:1
      我已尝试将此条件
      (int)character>47
      更改为
      (int)character>46
      ,但仍然收到相同的错误?!当我将结果存储在
      newLine
      数组字符串中并尝试执行以下操作时,我得到的erorro`for(string time:newLine){int hour=Character.getNumericValue(time.charAt(1));}`不确定您在做什么,因为无法按原样从答案中获取此错误。返回值将是数字前不带单词的字符串,并为空格进行修剪。尝试打印返回值并亲自查看。我已尝试了您的解决方案,但当字符为
      /
      时,我收到此错误
      java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:1
      我已尝试将此条件
      (int)character>47
      更改为
      (int)character>46
      但我还是会遇到同样的错误?!当我将结果存储在
      newLine
      数组字符串中并尝试执行以下操作时,我得到的erorro`for(string time:newLine){int hour=Character.getNumericValue(time.charAt(1));}`不确定您在做什么,因为无法按原样从答案中获取此错误。返回值将是数字前不带单词的字符串,并为空格进行修剪。尝试打印返回值并亲自查看。