Java 字符串拆分/删除 n 个特殊字符部分

标签 java string

输入

String s1 = "This Payer Ref [customerRef] does not exist/This Card Ref [cardRef] has already been used."

我想删除所有带有[variable]的部分

输出

"This Payer Ref does not exist/This Card Ref has already been used."

请注意,给定字符串中可以有 n 个[variable]

我尝试了很多方法,但没有成功。

最佳答案

你可以使用这个。

String s1 = "This Payer Ref [customerRef] does not exist/This Card Ref [cardRef] has already been used.";
s1=s1.replaceAll("\\[\\w+\\]", "");
System.out.println(s1);

关于Java 字符串拆分/删除 n 个特殊字符部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58949016/

相关文章:

java - 通过JAVA应用程序运行unix shell脚本

java - 如何正确部署 Bean - JNDI、Weblogic 12C

java - 使用自己的记录器刷新 System.out

python - 查找字符串中重复字符的最长子串

arrays - 从 C 中的 .txt 读取空行

java - 在 XML 文件中看不到命名空间前缀

java - jackson 多态性 : How to map multiple subtypes to the same class

regex - 提取两个相似字符串之间的行

c - 在 C 中没有匹配项时在字符串中插入一个字符

java - 在黑莓中将长字符串转换为字符串数组?