Java regex通配符不被接受

时间:2014-10-18 04:40:57

标签: java regex

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)

1 个答案:

答案 0 :(得分:1)

[\\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)
\(                       '('