如何从满足条件的文本中删除特定单词?

How to remove a particular word from a text that satisfies a condition?

我有一个包含很多单词的文本文件。我想删除包含重复字母的单词(例如 zoos - 包含 2 个 o)。执行此操作的最佳方法是什么?

Regular expressions 可能适合您。像

Pattern p = Pattern.compile("([a-zA-Z])*([a-zA-Z])\2([a-zA-Z])*");
Matcher m = p.matcher("zoo");
System.out.println(m.matches());

只需添加一个循环来尝试文件中的每个单词,如果 m.matches() == true - 删除它。

顺便说一句,如果你这样输入,这将不起作用

这是一个使用正则表达式和流的示例 api:

package demo;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Demonstration
{
    public static void main(String[] args)
    {
        List<String> input = Arrays.asList( //
            new String[] {"a", "bb", "ccc", "ded", "ff", "ghi", "jkll"});

        // Prints [a, ded, ghi]
        System.out.println(removeWordsWithRepetitiveCharacters(input));
    }

    private static List<String> removeWordsWithRepetitiveCharacters(List<String> words)
    {
        return words.stream() //
            .filter(word -> !word.matches(".*(\w)\1+.*")) //
            .collect(Collectors.toList());
    }
}