Answer the question
In order to leave comments, you need to log in
How to find the number of occurrences of characters from a regular expression in a string?
Regular expression - "\\W | _"
(any character except alphabetic and numeric).
You need to count the number of such characters in a string.
Answer the question
In order to leave comments, you need to log in
You have an error in the regular expression, parentheses are needed here:
String st = "q!w_e*"
Pattern pattern = Pattern.compile("(\\W)|(_)");
Matcher matcher = pattern.matcher(st);
long matches = matcher.results().count();
System.out.println(matches);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question