英文:
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<String> keywords) {
for (String keyword: keywords) {
String insensitiveWord = "(?i)\\b" + keyword + "\\b";
data = data.replaceAll(insensitiveWord, StringUtils.repeat("*", 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<String> keywords) {
for (String keyword : keywords) {
String insensitiveWord = "(?i)\\b" + keyword;
data = data.replaceAll(insensitiveWord, "*".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 *****
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论