Achow
Achow

Reputation: 8678

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)

Upvotes: 0

Views: 59

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174854

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

Upvotes: 1

Related Questions