简体   繁体   English

在定界符上拆分数组,但保留定界符的一部分

[英]Split array on delimiter but keep part of the delimiter

I am trying to split a string on the follow delimiter }--{ , but I would like to keep the braces. 我正在尝试在跟随定界符}--{上拆分字符串,但我想保留大括号。

Example: 例:

"{this}--{is}--{an}--{example}"  --> ["{this}","{is}","{an}","{example}"]

I can't split on the -- sequence on its own because some of the individual elements will have this sequence. 我不能单独对--序列进行拆分,因为某些单个元素将具有此序列。 Is this possible in Java? 这在Java中可行吗?

Since you would like to keep part of the delimiter, put the parts that you would like to keep into lookahead and lookbehind , as follows: 由于您想保留定界符的一部分,因此,请按如下所示将要保留的部分提前和往后看

String[] parts = str.split("(?<=\\})--(?=\\{)");

Demo. 演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM