Regex and escaped and unescaped delimiter

前端 未结 5 1556
我寻月下人不归
我寻月下人不归 2020-12-18 00:38

question related to this

I have a string

a\\;b\\\\;c;d

which in Java looks like

String s = \"a\\\\;b\\\\\\\\;c;d\"         


        
5条回答
  •  情深已故
    2020-12-18 01:16

    This approach assumes that your string will not have char '\0' in your string. If you do, you can use some other char.

    public static String[] split(String s) {
        String[] result = s.replaceAll("([^\\\\])\\\\;", "$1\0").split(";");
        for (int i = 0; i < result.length; i++) {
            result[i] = result[i].replaceAll("\0", "\\\\;");
        }
        return result;
    }
    

提交回复
热议问题