java Regex - split but ignore text inside quotes?

后端 未结 4 1996
深忆病人
深忆病人 2021-01-15 18:35

using only regular expression methods, the method String.replaceAll and ArrayList how can i split a String into tokens, but ignore delimiters that exist inside quotes? the

4条回答
  •  天涯浪人
    2021-01-15 19:17

    I know there is a damn good and accepted answer already present but I would like to add another regex based (and may I say simpler) approach to split the given text using any non-alphanumeric delimiter which not inside the single quotes using

    Regex:

    /(?=(([^']+'){2})*[^']*$)[^a-zA-Z\\d]+/
    

    Which basically means match a non-alphanumeric text if it is followed by even number of single quotes in other words match a non-alphanumeric text if it is outside single quotes.

    Code:

    String string = "hello^world'this*has two tokens'#2ndToken";
    System.out.println(Arrays.toString(
         string.split("(?=(([^']+'){2})*[^']*$)[^a-zA-Z\\d]+"))
    );
    

    Output:

    [hello, world'this*has two tokens', 2ndToken]
    

    Demo:

    Here is a live working Demo of the above code.

提交回复
热议问题