简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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