So I have a code here where I need to split incoming strings by the char ';'. However, there might be some that are escaped with
.
What I am doing then is to iterate it letter by letter for ; excluding if the previous letter was a
and then replace any outcomes where there was an escaped ;
with ;
.
This seems all a bit cumbersome to me, is there a better way how to do this?
public void parse(String line, Player player) {
if (line.contains(";")) { // check split sign
String subString;
int previousIndex = 0; // location of the first letter
String search = "\;";
String replace = ";";
// lets search for colons
int index = line.indexOf(';');
while (index >= 0) {
// check if the previous letter is a so we know it's escaped
if (line.charAt(index - 1) != '\') {
// get a substring for the current segment:
subString = substring(previousIndex, index);
if (subString.contains("/Command/")) { // Check if line is an actual command line
// replace escaped colons and execute command
parseCommand(subString.replaceAll(search, replace), player);
} else if (subString.contains("/Output/")) {
parseOutput(subString.replaceAll(search, replace), player);
} else {
Main.logDebugInfo(Level.WARNING, "Command parsing: No command or output tag found!");
}
previousIndex = index;
}
index = line.indexOf(';', index + 1); // next letter
}
} else {
Main.logDebugInfo(Level.WARNING, "Command parsing: No ; found.");
}
}
Alternative that comes to my mind would be to first replace all ;
with a Very specific substring (e.g. "%%€€", then split into a list by ;
and re-substitute the escaped ones with ;
. There is a tiny risk that this causes issues.
I am wondering if there is some standard routine/best practice to deal with escaped characters?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…