我想使用正则表达式删除字符串中所有出现的“ [某些文本]”模式。
到目前为止,这是我的代码:
public static void main(String[] args) {
String s = "asd bef [asd] gsg";
String s1 = "[asd] dasd";
String s2 = "asda [asd]";
String s3 = "asda [first] [second] third fourth [quatro] cinco";
String regex0 = "(.*)(\\[.*\\])+(.*)";
if (s.matches(regex0)) {
System.out.println("s0");
String tmp = s.replaceAll("(.*)(\\[.*\\])+(.*)","$1$3");
System.out.println(tmp);
}
if (s1.matches(regex0)) {
System.out.println("s1");
String tmp = s1.replaceAll("(.*)(\\[.*\\])+(.*)","$1$3");
System.out.println(tmp);
}
if (s2.matches(regex0)) {
System.out.println("s2");
String tmp = s2.replaceAll("(.*)(\\[.*\\])+(.*)","$1$3");
System.out.println(tmp);
}
if (s3.matches(regex0)) {
System.out.println("s3");
String tmp = s3.replaceAll("(.*?)(\\[.*\\])(.*?)","$1$3");
System.out.println(tmp);
}
}
这是我的代码的输出:
s0
asd bef gsg
s1
dasd
s2
asda
s3
asda cinco
对于String s3,结果应为:“ asda第三第四名cinco”部分。
关于我在做什么错或没有捕捉到的提示吗?
非常感谢