简体   繁体   English

基于开括号的Java中的Split String

[英]Split String in Java based on open braces

Existing string:

stringToBeCleaned = author(A, UniqueVar1), author(B, UniqueVar1);

Expected String:

stringAfterCleaned = author(A, UniqueVar1)^author(B, UniqueVar1);

The method I wanted to try was split the string and join it appending the exponential symbol. 我想尝试的方法是拆分字符串并连接它附加指数符号。 But I have problems in splitting. 但我在分裂方面遇到了问题。

split = stringToBeCleaned.split(", ");

This does not give the required split. 这不会提供所需的拆分。

split = stringToBeCleaned.split("), ");

This split throws error for the invalid regex, because the braces are not closed Is there any way to achieve this. 这个拆分抛出无效正则表达式的错误,因为大括号没有关闭有没有办法实现这一点。 Any suggestions would be helpful. 任何的意见都将会有帮助。

You just need to escape the parenthesis: 你只需要转义括号:

split = stringToBeCleaned.split("\\), ");

Or you can just replace the comma without regex: 或者你可以在没有正则表达式的情况下替换逗号:

stringToBeCleaned = stringToBeCleaned.replace("), ", ")^");

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

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