繁体   English   中英

如何在保留不同分隔符计数的同时拆分字符串?

[英]How to split a string while keeping a count of the different delimiters?

因此,输入看起来非常像{q1,q2},{a,b},{[q1:a:q2],[q2:b:q2]},q1,{q2} DFA。

我想做的是用逗号,方括号和冒号将其分开。 然后分别打印出结果(即大括号中的所有内容都有其自己的方法)。

例:

第1部分 = q1 q2

第2部分 = Ab

Part3 = q1通过

q2用b转到q2

第4部分 = q1

第5部分 = q2

我当时想的是保持大括号的计数,如果大括号的计数= 1、3、5等,它们将相应地执行这些方法。

问题是,当我将其用作字符串时,我无法确保它将“ q1”视为一个字符串,而不是“ q”“ 1”

当我使用.split(\\ s *,\\ s * | \\ {| \\} | \\ [| \\])分割字符串时,这些字符将被删除 ,我无法再继续对其进行计数。

或者我应该只保留花括号,打印一个子字符串(取下花括号),一旦看到一个紧密的花括号,它将移至下一个方法。

我试过的

分割字符串并存储到列表中

List<String> listDFA = Arrays.asList(DFA.split("\\s*,\\s*|\\{|\\}|\\[|\\]"));

启动DFA

for (index = 0; index<size; index++){
     if (curlyBrackets.contains(listDFA.get(index))){ //curlyBrackets has "{}"
        System.out.println("curly"); // just a test if it sees a curly will omit later
     }

     System.out.println(index); // again a test wanted to see what was being indexed
     System.out.println(listDFA.get(index));
  }

我想尝试的是:

for (index = 0; index <size; index++){
     if (curlyBrackets.contains(DFA.substring(index, index+1))){
        curly++;
        if (curly == 1){
           index++;
           states(DFA);
        }
     }
}

States()是:

public static void states (String DFA){
  //List<String> stateQ = new ArrayList<String>(Arrays.asList(DFA.split(" , "))); // I tried creating the list here, but then there would be a couple of incosistent variable such as index and size.
  lineVector = DFA.split(",");
  int size = lineVector.length;
  while(lineVector(index) != '}'){
     System.out.println(stateQ[index]); //DFA.charAt(index) lineVector[index] was trying either or...
     index++;
  }
  curly++;

我猜该问题有某种热修复,但是我仍然使用逗号作为分隔符(而不是括号)来分割字符串。

List<String> listDFA = Arrays.asList(DFA.split("\\s*,\\s*"));

因此,使字符串的每个内容都与一个索引相关联(括号被附加到最近的非逗号)。 然后将该索引存储到字符串中。

String currString = listDFA.get(index);

然后查看该字符串的任何部分是否有大括号

if (currString.indexOf('{') != -1 || currString.indexOf('}') != -1 )

并匹配大括号条件后,执行该块

if (curly <= 2){
           Q.add(currString);
     }

现在我需要弄清楚的是如何使用方括号执行此操作,因为我必须将其存储到2D数组中

暂无
暂无

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

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