[英]negate regex : not matching a specific word containing numbers
如果有人可以帮助,我想使用替换所有与单词不匹配的字符 => bac +[0-9]编辑
我试过了:
String test = "bac +5 - and plus";
test = test.replaceAll("^(?!.*bac \\+[0-9]).*","")
===>我想要结果测试=“bac +5”
使用后面看:
test = test.replaceAll("(?<=bac \\+\\d).*","");
您的负前瞻模式^(?..*bac \+[0-9]).*
表示匹配整行,如果它不包含此模式bac \+[0-9]
您可以对要保留的部分使用捕获组,并匹配应删除的行的 rest。
在替换中,使用组 1 和$1
String test = "bac +5 - and plus";
test = test.replaceAll("(bac \\+\\d).*","$1");
System.out.println(test);
Output
bac +5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.