简体   繁体   中英

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.

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

Part2 = ab

Part3 = q1 goes to q2 with a

q2 goes to q2 with b

Part4 = q1

Part5 = 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.

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"

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.

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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