Handling delimiter with escape characters in Java String.split() method

后端 未结 2 1324
无人及你
无人及你 2021-02-04 02:51

I have searched the web for my query, but didn\'t get the answer which fits my requirement exactly. I have my string like below:



        
2条回答
  •  佛祖请我去吃肉
    2021-02-04 03:24

    You can use Pattern.quote():

    String regex = "(?

    Using your example:

    String delim = "|";
    String regex = "(?
    A
    B
    C
    The Steading\|Keir Allan\|Braco
    E
    

    You can extend this to use a custom escape sequence as well:

    String delim = "|";
    String esc = "+";
    String regex = "(?
    A
    B
    C
    The Steading+|Keir Allan+|Braco
    E
    

提交回复
热议问题