简体   繁体   English

不接受Java正则表达式通配符

[英]Java regex wildcard not accepted

String depends2 = "success(job1) AND n(job2)";
depends2  = depends2.replaceAll("[\\s].(?i)[snd][\\s]*\\(", "");     
System.out.println(depends2);

I expect this to output 我希望这能输出

success(job1) ANDjob2)

Instead it outputs 相反,它输出

success(job1) AND n(job2)

[\\\\s].(?i)[snd] This in your regex ensures that there must a character present inbetween the space and n ( followed by zero or more spaces plus ( symbol ). But there isn't a character actually. So your regex fails and returns the original string without doing any replacements. [\\\\s].(?i)[snd]您的正则表达式中的这确保了空格和n之间必须有一个字符( 后跟零个或多个格加( symbol )。但实际上没有字符。因此,您的正则表达式将失败并返回原始字符串,而不进行任何替换。

String depends2 = "success(job1) AND n(job2)";
depends2  = depends2.replaceAll("\\s(?i)[snd]\\s*\\(", "");     
System.out.println(depends2);

Output: 输出:

success(job1) ANDjob2)

Explanation: 说明:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM