简体   繁体   English

Java字符串替换所有正则表达式

[英]Java string replaceAll regex

Hi I want to remove certain words from a long string, there problem is that some words end with "s" and some start with a capital, basically I want to turn: 嗨,我想从长字符串中删除某些单词,问题是有些单词以“ s”结尾,有些以大写字母开头,基本上我想转向:

"Hello cat Cats cats Dog dogs dog fox foxs Foxs"

into: 成:

"Hello"

at the moment I have this code but I want to improve on it, thanks in advance: 目前,我有这段代码,但是我想对其进行改进,在此先感谢:

                    .replace("foxs", "")
                    .replace("Fox", "")
                    .replace("Dogs", "")
                    .replace("Cats", "")
                    .replace("dog", "")
                    .replace("cat", "")

Try this: 尝试这个:

String input = "Hello cat Cats cats Dog dogs dog fox foxs Foxs";
input = input.replaceAll("(?i)\\s*(?:fox|dog|cat)s?", "");

Demo 演示

Maybe you can try to match everything except the word Hello . 也许您可以尝试匹配“ Hello ”一词以外的所有内容。 Something like: 就像是:

string.replaceAll("(?!Hello)\\b\\S+", "");

You can test it in this link . 您可以在此链接中对其进行测试。

The idea is to perform a negative lookahead for Hello word, and get any other word present. 这个想法是对Hello词执行否定的超前查找,并让其他任何词出现。

You can generate patterns that match all combinations for a word. 您可以生成与单词的所有组合匹配的模式。 Ie for dog you need the pattern [Dd]ogs? 即对于dog您需要[Dd]ogs?模式[Dd]ogs? :

  • [Dd] is a character class that matches both cases [Dd]是匹配两种情况的字符类
  • s? matches zero or one s 匹配零个或一个s
  • the rest of the word will be case sensitive. 该单词的其余部分将区分大小写。 Ie dOGS will not be a match. dOGS不会匹配。

This is how you can put it together: 这是将其组合在一起的方法:

public static void main(String[] args) {
    // it's easy to add any other word
    String original = "Hello cat Cats cats Dog dogs dog fox foxs Foxs";
    String[] words = {"fox", "dog", "cat"};
    String tmp = original;
    for (String word : words) {
        String firstChar = word.substring(0, 1);
        String firstCharClass = "[" + firstChar.toUpperCase() + firstChar.toLowerCase() + "]";
        String patternSrc = firstCharClass + word.substring(1) + "s?"; // [Ww]ords?
        tmp = tmp.replaceAll(patternSrc, "");
    }
    tmp = tmp.trim(); // to remove unnecessary spaces 
    System.out.println(tmp);
}

So you could pre-compile a list of the words you want and make it case insensitive something like: 因此,您可以预编译所需单词的列表,并使其不区分大小写,例如:

    String str = "Hello cat Cats cats Dog dogs dog fox foxs Foxs";
    Pattern p = Pattern.compile("fox[s]?|dog[s]?|cat[s]?", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(str);
    String result = m.replaceAll("");
    System.out.println(result);

[s]? [S]? handles if there is a plural form, where the ? 处理是否存在复数形式,其中? character will match 0 or 1 字符将匹配0或1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM