String depends2 = "success(job1) AND n(job2)";
depends2 = depends2.replaceAll("[\\s].(?i)[snd][\\s]*\\(", "");
System.out.println(depends2);
我希望输出
success(job1) ANDjob2)
相反,它输出
success(job1) AND n(job2)
最佳答案
[\\s].(?i)[snd]
正则表达式中的这可确保空格和 n
之间必须有一个字符 (后跟零个或多个空格加上 (
符号)。但实际上没有字符。因此您的正则表达式失败并返回原始字符串而不进行任何替换。
String depends2 = "success(job1) AND n(job2)";
depends2 = depends2.replaceAll("\\s(?i)[snd]\\s*\\(", "");
System.out.println(depends2);
输出:
success(job1) ANDjob2)
说明:
\s whitespace (\n, \r, \t, \f, and " ")
(?i) set flags for this block (case-
insensitive) (with ^ and $ matching
normally) (with . not matching \n)
(matching whitespace and # normally)
[snd] any character of: 's', 'n', 'd'
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times)
\( '('
关于Java 正则表达式通配符不被接受,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26436356/