组合正则表达式组并搜索匹配/搜索多个正则表达式组

时间:2017-05-29 01:35:07

标签: java regex

我现在拥有的:

private static final String[] CD_LISTING = new String[] {
        "first", "second", "third", "furthermore", "finally",
        "to begin", "to conclude", "next"
};
private static final String[] CD_REINFORCEMENT = new String[] {
        "also", "furthermore", "moreover", "what is more",
        "in addition", "besides", "above all", "as well as",
        "as well", "in the same way"
};
private static final String LISTING =
        "(?i)\\b(" + String.join("|", Arrays.CD_LISTING) + ")\\b";
private static final String REINFORCEMENT =
        "(?i)\\b(" + String.join("|", Arrays.CD_REINFORCEMENT) + ")\\b";

private static int patternMatcher(String matcherGroup, Pattern pattern) {
    int occurrence = 0;
    Matcher matcher = pattern.matcher(Text.getTextToAnalyse());
    while (matcher.find()) {
        if (matcher.group(matcherGroup) != null) occurrence++;
    }
    return occurrence;
}
public static final Pattern CD_PATTERN = Pattern.compile(
        "(?<LISTING>" + LISTING + ")|(?<REINFORCEMENT>" + REINFORCEMENT + ")")

我使用patternMatcher("LISTING", CD_PATTERN)搜索LISTINGpatternMatcher("REINFORCEMENT", CD_PATTERN)的出现,以搜索&#34; REINFOREMENT&#34;的出现。但是,由于我有很多小组,因此创建另一个小组将是愚蠢的。有没有办法在不重复元素的情况下组合这些组?

e.g。 &#34;此外&#34;是组中的重复元素。

我想搜索总发生次数。但是,如果我只是加起来LISTINGREINFORCEMENT,则会重复使用&#34;此外&#34;将被计算两次。

有没有办法一次搜索多个组或组合这些匹配组?非常感谢

0 个答案:

没有答案