Regex split string but keep separators

Use zero-length maching lookarounds; you want to split on

(?=\[)|(?<=\])

That is, anywhere where we assert a match of a literal [ ahead, or where we assert a match of literal ] behind.

As a C# string literal, this is

@"(?=\[)|(?<=\])"

See also

  • regular-expressions.info/Lookarounds

Related questions

  • Java split is eating my characters. — has many examples

Example in Java

    System.out.println(java.util.Arrays.toString(
        "abc[s1]def[s2][s3]ghi".split("(?=\\[)|(?<=\\])")
    ));
    // prints "[abc, [s1], def, [s2], [s3], ghi]"

    System.out.println(java.util.Arrays.toString(
        "abc;def;ghi;".split("(?<=;)")
    ));
    // prints "[abc;, def;, ghi;]"

    System.out.println(java.util.Arrays.toString(
        "OhMyGod".split("(?=(?!^)[A-Z])")
    ));
    // prints "[Oh, My, God]"

Leave a Comment

tech