[英]How to split a string while keeping a count of the different delimiters?
So the input looks like this {q1,q2},{a,b},{[q1:a:q2],[q2:b:q2]},q1,{q2} A DFA pretty much. 因此,输入看起来非常像{q1,q2},{a,b},{[q1:a:q2],[q2:b:q2]},q1,{q2} DFA。
What I would like to do is split it through the comma, brackets, and colon. 我想做的是用逗号,方括号和冒号将其分开。 then print out the results individually (ie. everything in the curly brackets have their own method).
然后分别打印出结果(即大括号中的所有内容都有其自己的方法)。
Example: 例:
Part1 = q1 q2 第1部分 = q1 q2
Part2 = ab 第2部分 = Ab
Part3 = q1 goes to q2 with a Part3 = q1通过
q2 goes to q2 with b q2用b转到q2
Part4 = q1 第4部分 = q1
Part5 = q2 第5部分 = q2
What I was thinking was keeping a count of curly braces and if the count of curly braces =1,3,5, etc... they will execute those methods accordingly. 我当时想的是保持大括号的计数,如果大括号的计数= 1、3、5等,它们将相应地执行这些方法。
Problem is, when I use it as a string, I have no way of making sure it will consider "q1" as one string rather than "q" and "1" 问题是,当我将其用作字符串时,我无法确保它将“ q1”视为一个字符串,而不是“ q”和“ 1”
When I split the string by using .split(\\s*,\\s*|\\{|\\}|\\[|\\]) it will take those characters off and I can no longer keep count of them. 当我使用.split(\\ s *,\\ s * | \\ {| \\} | \\ [| \\])分割字符串时,这些字符将被删除 ,我无法再继续对其进行计数。
Or should I just keep the curly braces, print a substring (taking off the curly brace), once it sees a close curly brace it will move to the next method. 或者我应该只保留花括号,打印一个子字符串(取下花括号),一旦看到一个紧密的花括号,它将移至下一个方法。
What I've tried: 我试过的
Splitting the string and storing into a list 分割字符串并存储到列表中
List<String> listDFA = Arrays.asList(DFA.split("\\s*,\\s*|\\{|\\}|\\[|\\]"));
Starting DFA 启动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));
}
What I wanted to try: 我想尝试的是:
for (index = 0; index <size; index++){
if (curlyBrackets.contains(DFA.substring(index, index+1))){
curly++;
if (curly == 1){
index++;
states(DFA);
}
}
}
And states() is: 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++;
I guess some kind of a hot-fix for the problem, but I still split the string using comma as a delimiter (instead of the brackets). 我猜该问题有某种热修复,但是我仍然使用逗号作为分隔符(而不是括号)来分割字符串。
List<String> listDFA = Arrays.asList(DFA.split("\\s*,\\s*"));
Therefore making each content of the string be associated with an index (brackets are attached to the nearest non-comma). 因此,使字符串的每个内容都与一个索引相关联(括号被附加到最近的非逗号)。 Then store that index into a string.
然后将该索引存储到字符串中。
String currString = listDFA.get(index);
Then see if any part of that string has a curly bracket 然后查看该字符串的任何部分是否有大括号
if (currString.indexOf('{') != -1 || currString.indexOf('}') != -1 )
And once it matches the curly bracket condition, execute that block 并匹配大括号条件后,执行该块
if (curly <= 2){
Q.add(currString);
}
Now all I have to figure out is how to do that with square brackets since I have to store those to a 2D array 现在我需要弄清楚的是如何使用方括号执行此操作,因为我必须将其存储到2D数组中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.