I have java code with matcher to find number of occurence in string using mattcher.find method.
following is my code
String text = "INCLUDES(ABC) EXCLUDES(ABC) EXCLUDES(ABC) INCLUDES(EFG) INCLUDES(IJK)";
String patternString = "INCLUDES(.)";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);
int count = 0;
while(matcher.find()) {
count++;
System.out.println("found: " + count + " : "
+ matcher.start() + " - " + matcher.end());
System.out.println(" - " +text.substring(matcher.start(), matcher.end()));
}
which returns output as
found: 1 : 0 - 9
- INCLUDES(
found: 2 : 42 - 51
- INCLUDES(
found: 3 : 56 - 65
- INCLUDES(
Instead of i want Regex to find and return number of occurrences as INCLUDES(*)
any solution is appriciated. expected output should be loop printing values
INCLUDES(ABC)
INCLUDES(EFG)
INCLUDES(IJK)
Your regex is not correct. You are just capturing a single character inside the bracket, and hence your regex will fail.
Try using this: –
And then get
group(0), or justgroup(): –