简体   繁体   中英

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("), ", ")^");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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