Can you use zero-width matching regex in String split?

后端 未结 3 1074
后悔当初
后悔当初 2020-11-30 14:17
System.out.println(
    Arrays.deepToString(
        \"abcghi\".split(\"(?:<)|(?:>)\")
    )
);

This prints [abc, def, ghi]<

3条回答
  •  感动是毒
    2020-11-30 15:00

    Thanks to information from Cine, I think these are the answers I'm looking for:

    System.out.println(
        Arrays.deepToString(
            "abcghi".split("(?=<)|(?<=>)")
        )
    ); // [abc, , ghi, , ]
    
    
    System.out.println(
        Arrays.deepToString(
            "Hello! Oh my!! Good bye!! IT WORKS!!!".split("(?<=!++)")
        )
    ); // [Hello!,  Oh my!!,  Good bye!!,  IT WORKS!!!]
    

    Now, the second one was honestly discovered by experimenting with all the different quantifiers. Neither greedy nor reluctant work, but possessive does.

    I'm still not sure why.

提交回复
热议问题