假设我的正则表达式是这样的
Pattern ANIMALS = Pattern.compile("boy|dog|cat|hen")
我的字符串是
String = "The boy is good";
正如"boy"
我的模式中存在的那样,它会匹配但有什么方法可以将“男孩”返回给我。请不要建议制作 Set 或 ArrayList 并将其与 contains 方法匹配,因为我的模式非常庞大,我使用的只是一个非常小的示例。
matcher.group()
一旦你摆脱Matcher
它,你就可以使用方法Pattern
Matcher m = ANIMALS.matcher("The boy is good");
while(m.find()) {
System.out.println(m.group());
}