gpt4 book ai didi

java - 不区分大小写的搜索和替换

转载 作者:行者123 更新时间:2023-12-03 18:20:58 25 4
gpt4 key购买 nike

我有以下字符串,如何在Java中搜索和替换它?

之前

*animal is a *ANImal and *Bird is a *bIrd.


搜索并替换后,应该是*动物=狗和*鸟=孔雀

Dog is a Dog and Peacock is a Peacock.


我尝试用这种模式替换出现-(?i)\\ * animal但它不起作用。我究竟做错了什么?

最佳答案

   public String replaceStrPattern(String str, String pattern, String replaceWith) {

String newStr = str;
String nonAlphaNumeric = "[^a-zA-Z0-9]";

String[] words = str.split(" ");
for(String word : words) {
word = word.replaceAll(nonAlphaNumeric, "").trim();
if(word.equalsIgnoreCase(pattern))
newStr = newStr.replace(word, replaceWith);
}

return newStr;
}

关于java - 不区分大小写的搜索和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8025273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com