防止在字符串JAVA中重复子字符串搜索

时间:2017-04-05 14:16:30

标签: java regex

这就是我所做的:

String[] conjunctions = new String[] {
            "after", "although", "as if",
            "as long as", "as much as", "as soon as",
            "as though", "because", "before", "by the time",
            "even if", "even though", "if",

    };
    for (String toSearch : conjunctions) {
        int occurrence = textToAnalyse.split("(?i)\\W" + toSearch +"\\W").length - 1;
        System.out.println(toSearch + " X " + occurrence);
    }
例如,"if""even if"将被视为同一事物,因为"if"将被计算两次。有没有办法将搜索优先级排序到后者,并阻止JAVA搜索两次?非常感谢

输入:

textToAnalyse = "Even if you are smart, you are still dumb."

预期产出:

even if X 1
if X 0

1 个答案:

答案 0 :(得分:1)

你应该稍微改变一下你的方法。考虑将连词定义为互斥正则表达式列表,然后计算匹配数。作为一个快乐的副作用,这种方法也将消除构建大量子串的需要,这有助于内存使用。