简体   繁体   English

Java:不等分线

[英]Java: Split lines that are unequal

I tried looking for answers online, but I don't know how to word it correctly to find what I'm looking for. 我尝试过在线寻找答案,但是我不知道如何正确地用词表达自己的意思。 I have a file that I need to split but some lines are missing the regex I am trying to use. 我有一个文件需要拆分,但是有些行缺少我要使用的正则表达式。 The file I need to split looks like this: 我需要拆分的文件如下所示:

A,106,Chainsaw 12"
D,102
d,104
a,107,Chainsaw 10"

I need to split it in three different sections, Letter, ID, Tool but 102 and 104 are missing the comma and Tool section. 我需要将其分为三个不同的部分,即Letter,ID,Tool,但是102和104缺少逗号和Tool部分。 I've tried: 我试过了:

String[] sec = line.split(",");

and

String[] sec = line.split(",| \n");

And several other regex combinations, but none of them work. 和其他几种正则表达式组合,但它们都不起作用。 I get an AOB error on the line such as (below) because its missing. 我在(如下面)这样的行上收到一个AOB错误,因为它丢失了。

...[0];
...[1];
String tool = sec[2]; //here

Any help is appreciated 任何帮助表示赞赏

Use String[] sec = line.split(","); 使用String[] sec = line.split(","); and then test the length of the sec array 然后测试sec数组的长度

If you have 2 then you can use sec[0] and sec[1] but if you have 3 you can also use sec[2] If you have 0 then you have a empty line 如果您有2,则可以使用sec[0]sec[1]但是如果您有3,则也可以使用sec[2]如果您有2,则可以使用空行

You were on the right path with 你走在正确的道路上

String[] sec = line.split(",");

This function will return an Array with (in your example) either 2 or 3 elements. 此函数将返回一个数组(在您的示例中)为2或3个元素。 Then you could simply check for the length of the array 然后,您可以简单地检查数组的长度

String[] sec = line.split(",");
int length = sec.length;

Here you can see what type of entry you have. 在这里,您可以查看自己的输入类型。 Either length will be 2, which mean that it is a letter-ID pair, or it is 3, which means it is a letter, Id and tool entry. 长度将为2,这意味着它是字母ID对,或者为3,这意味着它是字母,Id和工具条目。

If you need to be able to distinguish between more categories, you will have to put in an extra check. 如果您需要能够区分更多类别,则必须进行额外检查。 For example: Lets say your entry can be missing out one of the other two elements (not only the tool) and you could encounter an entry like: 例如:假设您的条目可能缺少其他两个元素之一(不仅是工具),而且您可能会遇到类似以下条目:

a,Chainsaw 10

In this case you will furthermore need to read out the type of the single elements. 在这种情况下,您还需要读取单个元素的类型。 The first thing that comes to my mind is, that you could check the first element in your array and check its length (should always be 1, since it is just a letter) and parse the second one into Integer (I assume id is always a number) 我想到的第一件事是,您可以检查数组中的第一个元素并检查其长度(应始终为1,因为它只是一个字母),然后将第二个元素解析为Integer(我认为id始终为一个号码)


You are splitting on , and you have cases where input string is not present in same pattern. 您正在分割,并且遇到输入字符串不以相同模式出现的情况。

So for this knid of situation, after splitting the string, every-time you have to check for the array length. 因此,对于这种情况,在拆分字符串之后,每次都必须检查数组长度。
If it's less than desired length, then you cannot access desired element, because it's not present in the array. 如果它小于所需的长度,那么您将无法访问所需的元素,因为它不在数组中。

For example: 例如:

  • When you do String[] sec = line.split(","); 当您执行String[] sec = line.split(","); for A,106,Chainsaw 12 , you will have 3 length, and you can access elements like sec[0],sec[1],sec[2] . 对于A,106,Chainsaw 12 ,您将拥有3长度,并且可以访问sec[0],sec[1],sec[2]类的元素。
  • When you split A,106 , then you will get 2 as length, and elements present in the array are going to be sec[0],sec[1] . 当拆分A,106 ,您将获得2作为长度,并且数组中存在的元素将为sec[0],sec[1]



Example code: 示例代码:

import java.util.*;
public class ArrayListDemo {

   public static void main(String args[]) {
     ArrayList<String> lines= new ArrayList<String>();
         lines.add("A,106,Chainsaw 12");
         lines.add("A,106");
          lines.add("A");

       for(String str:lines){
         String[] parts = str.split(",");
         if(parts.length<2){
             for(int i = 0 ; i < parts.length ; i++)
             System.out.println("Splitted Item at index " + "[" + i + "]" + "::" + parts[i]);
         }else{
             for(int i = 0 ; i < parts.length ; i++)
             System.out.println("Splitted Item at index " + "[" + i + "]" + "::" + parts[i]);
         }
       }


   }
}


Hope that helps.. 希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM