Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

How to split a string by a string in Java, considering escaped ones

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

split takes regex as parameter, so you can use negative lookbehind:

String[] split = foo.split("(?<!\\);");

Yes, that's 4 's repeated, because each needs to be escaped.

I am wondering if there is some standard routine/best practice to deal with escaped characters?

Quote your values, or use a separator that doesn't appear in actual content. Or better yet, use some well-defined format for transmitting data, such as JSON.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...