[英]Java split regexp when delimiter is part of the data
Sorry if this question has already been solved, or closed but I have been searching for long without an answer. 抱歉,这个问题已经解决,或者已经结案,但是我一直在寻找很长时间没有答案。
I have to split lines I am receiving from an external systems, using the ~
delimiter. 我必须使用
~
分隔符分割从外部系统接收的行。
I have an issue because some data contain ~~
(~ repeated twice) and in this case the data must not be split. 我有一个问题,因为某些数据包含
~~
(〜重复两次),在这种情况下,不得拆分数据。
So if I receive A~B~C~~C~D
I want this split back: A, B, C~~C, D
因此,如果我收到
A~B~C~~C~D
我想将其拆分回去: A, B, C~~C, D
I cannot figure out what regular expression I have to used not to split ~~
. 我不知道我必须使用什么正则表达式才能拆分
~~
。
You can split by 你可以分割
\b~\b
See demo. 参见演示。
https://regex101.com/r/t3D2Jp/1 https://regex101.com/r/t3D2Jp/1
You can use 您可以使用
(?:^|\b)~(?:$|\b)
if you want to remove trailing ones too 如果您也要删除尾随的
You can use (?<!~)~(?!~)
with a negative look-ahead and look-behind for ~
. 您可以使用
(?<!~)~(?!~)
进行负向前看并向后看~
。
Example 例
String test = "A~B~C~~D~E";
System.out.println(
Arrays.toString(
test.split("(?<!~)~(?!~)")
)
);
Output 产量
[A, B, C~~D, E]
This should also work with more than two consecutive ~
s, eg with "A~B~C~~~D~E"
. 这也应该适用于两个以上的连续
~
,例如,使用"A~B~C~~~D~E"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.