如何在Java中用特殊字符替换字符串中的单词?

huangapple go评论69阅读模式
英文:

How to replace word with special character from string in java?

问题

我正在编写一个方法,应该将与列表中的单词匹配的所有单词替换为'****'。

到目前为止,我的代码可以很好地处理没有特殊字符的字符串,但所有带有特殊字符的字符串都会被忽略。

以下是迄今为止我拥有的代码:

public static String maskKeywordData(String data, List<String> keywords) {
  for (String keyword: keywords) {
    String insensitiveWord = "(?i)\\b" + keyword + "\\b";
    data = data.replaceAll(insensitiveWord, StringUtils.repeat("*", keyword.length()));
  }
  return data;
}

例如,我有一个关键词列表 ['c.u.n.t', 'a$$', 'bitch']。

输入: "Hello c.u.n.t with a$$ and bitch"

期望的输出: "Hello ******* with *** and *****"

英文:

I am writing a method that should replace all words which match with ones from the list with '****'.

So far I have code that works fine with strings without special characters but all string with special characters are ignoring.

Here's the code I have so far:

public static String maskKeywordData(String data, List&lt;String&gt; keywords) {
  for (String keyword: keywords) {
			String insensitiveWord = &quot;(?i)\\b&quot; + keyword + &quot;\\b&quot;;
			data = data.replaceAll(insensitiveWord, StringUtils.repeat(&quot;*&quot;, keyword.length()));
		}
		return data;
	}

E.g. I have a list of keywords ['c.u.n.t', 'a$$', 'bitch'].

Input : "Hello c.u.n.t with a$$ and bitch"

Expected output : "Hello ******* with *** and *****"

答案1

得分: 1

你可以使用以下代码:

public static String maskKeywordData(String data, List<String> keywords) {
    for (String keyword : keywords) {
        String insensitiveWord = "(?i)\\b" + keyword;
        data = data.replaceAll(insensitiveWord, "*".repeat(keyword.length()));
    }
    return data;
}

它唯一的缺点是,在关键词列表中,对于 $,你必须使用\\$在单词中,例如,a$$ 在列表中必须写成 a\\$\\$ 才能让上述代码运行。

原因是,\\b 表示单词边界,它检查 (^ \w|\w $|\W \w|\w \W),因此,单词 a$$ 不会被匹配。

以上代码的输出为:

Hello ******* with **** and *****

英文:

You can use

public static String maskKeywordData(String data, List&lt;String&gt; keywords) {
	for (String keyword : keywords) {
		String insensitiveWord = &quot;(?i)\\b&quot; + keyword;
		data = data.replaceAll(insensitiveWord, &quot;*&quot;.repeat(keyword.length()));
	}
	return data;
}

Its only downside is, in the list, for $ you have to use\\$ in the word, like a$$ will be a\\$\\$ in the list for the above code to run.
Reason is, \\b is word boundary which checks for (^ \w|\w $|\W \w|\w \W), due to which the word a$$ is not taken.
Output by the above code:
Hello ******* with **** and *****

huangapple
  • 本文由 发表于 2023年3月7日 01:13:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653833.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定