How to split a string, but also keep the delimiters?

前端 未结 23 2322
我在风中等你
我在风中等你 2020-11-21 06:32

I have a multiline string which is delimited by a set of different delimiters:

(Text1)(DelimiterA)(Text2)(DelimiterC)(Text3)(DelimiterB)(Text4)
23条回答
  •  野的像风
    2020-11-21 06:57

    You can use Lookahead and Lookbehind. Like this:

    System.out.println(Arrays.toString("a;b;c;d".split("(?<=;)")));
    System.out.println(Arrays.toString("a;b;c;d".split("(?=;)")));
    System.out.println(Arrays.toString("a;b;c;d".split("((?<=;)|(?=;))")));

    And you will get:

    [a;, b;, c;, d]
    [a, ;b, ;c, ;d]
    [a, ;, b, ;, c, ;, d]

    The last one is what you want.

    ((?<=;)|(?=;)) equals to select an empty character before ; or after ;.

    Hope this helps.

    EDIT Fabian Steeg comments on Readability is valid. Readability is always the problem for RegEx. One thing, I do to help easing this is to create a variable whose name represent what the regex does and use Java String format to help that. Like this:

    static public final String WITH_DELIMITER = "((?<=%1$s)|(?=%1$s))";
    ...
    public void someMethod() {
    ...
    final String[] aEach = "a;b;c;d".split(String.format(WITH_DELIMITER, ";"));
    ...
    }
    ...
    

    This helps a little bit. :-D

提交回复
热议问题