简体   繁体   English

使用多个唯一的分隔符分割字符串

[英]Split a string with multiple unique delimiters

I have string in this format 我有这种格式的字符串

x/1.25 o/33.3 description for item A`

Now, I want to split it so it becomes 现在,我想将其拆分成

1.25, 33.3, description for item A

So far, I used .split(x/|o/) which works for this case. 到目前为止,我使用了适用于这种情况的.split(x/|o/) However, it becomes invalid if the user put x/ or o/ in the description of item A , like description o/ item A . 但是,如果用户将x/o/放在description of item Adescription of item A (如description o/ item A ,则该设置无效。

Is there any better regex that utilize the order of the parameter and delimiter in above string format? 是否有更好的正则表达式以上述字符串格式利用参数和定界符的顺序? Thanks in advance. 提前致谢。

One possible way is to do 一种可能的方法是

String[] fields = input.split("\\s+",3);

which will split into 3 blank-delimited fields. 它将分为3个以空格分隔的字段。 Then apply your regex to only the first two and reassemble the result into the output. 然后将您的正则表达式仅应用于前两个,并将结果重新组合到输出中。

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

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