我有带有 matcher 的 java 代码,可以使用 matcher.find 方法查找字符串中的出现次数。
以下是我的代码
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()));
}
返回输出为
found: 1 : 0 - 9
- INCLUDES(
found: 2 : 42 - 51
- INCLUDES(
found: 3 : 56 - 65
- INCLUDES(
而不是我希望正则表达式查找并返回出现次数为 INCLUDES(*)
任何解决方案都适用。预期输出应该是循环打印值
INCLUDES(ABC)
INCLUDES(EFG)
INCLUDES(IJK)