简体   繁体   English

使用正则表达式将字符串按列解析为列表,以模仿Java中的表

[英]Parsing a string into a list by column to mimic a table in Java, using regex

What I have: 我有的:

I have a string that looks like this 我有一个看起来像这样的字符串

"a, b, c\nd, e, f\ng, h, i"

each new line represents a new row, each comma represents a different column 每行代表一个新行,每个逗号代表一个不同的列

What I want to do: 我想做的事:

I'm trying to pass this into a list so that it looks like this: 我正在尝试将其传递到列表中,使其看起来像这样:

[ [a,d,g], [b,e,h], [c,f,i] ]

that way it mimics a table, a table that would look like this: 这样,它就可以模拟一个表,该表如下所示:

a   b   c
d   e   f
g   h   i

In other words, I'm making a list of each column. 换句话说,我正在列出每一列。

What I know how to do: 我知道该怎么做:

I know if I were making a list of rows I could do this: 我知道是否要创建行列表,我可以这样做:

String rows[]= text.split("\\r?\\n");

and end up with an array of rows: 并以一个行数组结束:

{"a, b, c", "d, e, f", "g, h, i"}

and then format each string as a list later but how would I accomplish what I want stated above using a regex with 1 or no loop? 然后稍后将每个字符串格式化为列表,但是如何使用带有1或无循环的正则表达式来完成我想要的内容呢? I'm trying for O(n) or O(nlogn) algorithm where n is the number of rows. 我正在尝试O(n)O(nlogn)算法,其中n是行数。 Thank you :) 谢谢 :)

What I'm currently doing: 我目前正在做什么:

 //This is pseudocode so it's not valid

 new list of list called columnList
 String rows[]= text.split("\\r?\\n");
 for each row in rows:
     i=0
     new splitRow = row split by comma
     for each element in splitRow:
         columnList(i).add(element)
         i++

This is the algorithm I came up with but it's really slow and requires nested loops. 这是我想出的算法,但是它确实很慢,并且需要嵌套循环。 I don't think it's very efficient. 我认为效率不是很高。

Further Info: 更多信息:

I'm doing all this in Java 我正在用Java做这一切

I can't use any Hash structures so no dictionaries sadly 我无法使用任何哈希结构,因此可悲的是没有字典

Sorry if my formatting is oddly formal, I've had problems communicating my questions clearly in the past. 抱歉,如果我的格式化格式很奇怪,过去我在交流问题时遇到了问题。

I think the steps should be: 我认为步骤应该是:

  1. Replace "\\n" in the original String 替换原始字符串中的“ \\ n”
  2. Split the "," to the String[] 将“,”分割为String []
  3. Convert one dimension array to two dimension array. 将一维数组转换为二维数组。

Something looks like: 看起来像:

        String s = "a, b, c\nd, e, f\ng, h, i";
        String[] elements = s.replace("\n", ",").split(",");
        String[][] table = new String[3][3];
        for (int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++){
                table[i][j] = elements[(j * 3) + i];
            }
        }

Hope this help. 希望能有所帮助。

Use your solution with the nested loops. 将解决方案与嵌套循环一起使用。 If you're worried that it isn't O(n), don't be: it is O(n). 如果您担心它不是O(n),请不要:它 O(n)。 The time it takes will always be exactly proportional to the number of input elements. 花费的时间将始终与输入元素的数量成正比。

Concrete example #1 : In your 3-row, 3-column input, you have 9 elements total. 具体示例1 :在3行3列输入中,总共有9个元素。 The outer loop gets executed 3 times. 外循环执行3次。 The inner loop executes 3 times for each of those, a total of 9 times. 内部循环将为每个循环执行3次,共执行9次。 9 inputs = 9 inner loop executions. 9个输入= 9个内部循环执行。

Concrete example #2 : Now let's say you give it a 5-row, 7-column input. 具体示例2 :现在让我们为它提供5行7列的输入。 You have 35 items total. 您总共有35件物品。 The outer loop executes five times. 外循环执行五次。 The inner loop execute seven times for each of those, a total of 5x7=35 times. 内循环对每个循环执行7次,总共5x7 = 35次。 35 inputs = 35 inner loop executions. 35个输入= 35个内部循环执行。

Abstract : How about an input of r rows by c columns? 摘要 :如何通过c列输入r行? Total input items = r x c . 总输入项= r x c Outer loop executes r times, inner loop executes c times for each of those. 外循环执行r次,内循环分别执行c次。 Thus, r x c total inner loop iterations. 因此, r x c总共是内部循环迭代。 r x c input items = r x c inner loop iterations. r x c个输入项= r x c个内循环迭代。

Whatever the total number of input elements, the inner loop executes exactly that many times. 无论输入元素的总数是多少,内部循环都会执行多次。 That is virtually the definition of O(n) . 这实际上是O(n)定义

You can try this code 您可以尝试此代码

    String text = "a, b, c\nd, e, f\ng, h, i";
    Pattern pattern = Pattern.compile("(.+)(?:\r?\n|$)");
    Matcher matcher = pattern.matcher(text);
    List<List<String>> result = Lists.newArrayList();
    while (matcher.find()){
        List<String> row = Splitter.on(",").trimResults().splitToList(matcher.group(1));
        result.add(row);
    }

    System.out.println(result);

remember import: 记住导入:

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;

Hope this help. 希望能有所帮助。

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

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