Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式,用于查找出现在一起的两个单词_Java_Regex - Fatal编程技术网

Java正则表达式,用于查找出现在一起的两个单词

Java正则表达式,用于查找出现在一起的两个单词,java,regex,Java,Regex,我正在尝试编写一个正则表达式,它将计算字符串中两个单词在一定距离内(彼此之间的距离不超过5个单词)同时出现的次数,而不会重复计算单词 例如,如果我有一个字符串: “这个人喜欢他的大帽子。这顶帽子非常大。” 在这种情况下,正则表达式应该在第一句中看到“big hat”,在第二句中看到“hats is big”,总共返回2。请注意,在第二个句子中,“hat”和“big”之间有几个词,它们的出现顺序也与第一个句子不同,但它们仍然出现在5个词的窗口内 如果正则表达式不是解决这个问题的正确方法,请告诉我应

我正在尝试编写一个正则表达式,它将计算字符串中两个单词在一定距离内(彼此之间的距离不超过5个单词)同时出现的次数,而不会重复计算单词

例如,如果我有一个字符串:

“这个人喜欢他的大帽子。这顶帽子非常大。”

在这种情况下,正则表达式应该在第一句中看到“big hat”,在第二句中看到“hats is big”,总共返回2。请注意,在第二个句子中,“hat”和“big”之间有几个词,它们的出现顺序也与第一个句子不同,但它们仍然出现在5个词的窗口内

如果正则表达式不是解决这个问题的正确方法,请告诉我应该尝试什么

如果正则表达式不是解决这个问题的正确方法,请告诉我应该尝试什么

正则表达式可能会起作用,但它们不是最好的方法

更好的方法是将输入字符串拆分为一个单词序列(例如,使用
string.split(…)
),然后循环遍历该序列,如下所示:

String[] words = input.split("\\s");
int count = 0;
for (int i = 0; i < words.length; i++) {
    if (words[i].equals("big")) {
        for (int j = i + 1; j < words.length && j - i < 5; j++) {
            if (words[j].equals("hat")) {
                count++;
            }
        }
    }
}
// And repeat for "hat" followed by "big".
String[]words=input.split(\\s”);
整数计数=0;
for(int i=0;i
你可能需要根据你想数到的数字来改变,但这只是一般的想法



如果你需要对很多很多词进行组合,那么寻找一个更有效的解决方案是值得的。但是,作为一次性或低容量的用例,最简单的是最好的。

有点像Stephen C,但使用库类来辅助力学

    String input = "The man liked his big hat. The hat was very big";
    int proximity = 5;

    // split input into words
    String[] words = input.split("[\\W]+");

    // create a Deque of the first <proximity> words
    Deque<String> haystack = new LinkedList<String>(Arrays.asList(Arrays.copyOfRange(words, 0, proximity)));

    // count duplicates in the first <proximity> words
    int count = haystack.size() - new HashSet<String>(haystack).size();
    System.out.println("initial matches: " + count);

    // process the rest of the words
    for (int i = proximity; i < words.length; i++) {
        String word = words[i];
        System.out.println("matching '" + word + "' in [" + haystack + "]");

        if (haystack.contains(word)) {
            System.out.println("matched word " + word + " at index " + i);
            count++;
        }

        // remove the first word
        haystack.removeFirst();
        // add the current word
        haystack.addLast(word);
    }

    System.out.println("total matches:" + count);
String input=“这个男人喜欢他的大帽子,帽子非常大”;
int接近度=5;
//将输入拆分为单词
String[]words=input.split([\\W]+”);
//创建第一个单词的名称
dequehaystack=newlinkedlist(Arrays.asList(Arrays.copyOfRange(words,0,approxity));
//计算第一个单词中的重复项
int count=haystack.size()-新HashSet(haystack.size();
System.out.println(“初始匹配:“+count”);
//处理剩下的单词
for(int i=接近度;i
此正则表达式将匹配两个单词的每一次出现,两个单词在5个单词内同时出现

([a-zA-Z]+)(?:[^ ]* ){0,5}\1[^a-zA-Z]
  • ([a-zA-Z]+)
    将匹配word,如果您能将其匹配到您的word中的[0-9],您可以替换([a-zA-Z0-9]+)

  • (?:[^]*){0,5}
    以匹配0到5个单词

  • \1[^a-zA-Z]
    以匹配单词的重复


然后,您可以将其与一个模式一起使用,并找到重复出现的每个单词。。。其他答案中的所有代码。。。这个单线解决方案怎么样:

int count = input.split("big( \\b.*?){1,5}hat").length + input.split("hat( \\b.*?){1,5}big").length - 2;

我曾考虑过类似的事情,但这似乎有点野蛮,我也相信它最终会重复计算一些单词。你需要知道你在寻找哪些单词。