gpt4 book ai didi

java - 如何从输入文本中删除特殊字符

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:13 25 4
gpt4 key购买 nike

我想从输入文本中删除所有特殊字符以及一些受限制的单词。

无论我想删除什么,它都会动态出现

(让我澄清一下:无论我需要排除哪些单词,它们都将动态提供 - 用户将决定需要排除哪些单词。这就是我没有包含正则表达式的原因。restricted_words_list(请参阅我的代码)将从数据库获取,只是为了检查我静态保存的代码是否正常工作),

但出于演示目的,我将它们保存在字符串数组中,以确认我的代码是否正常工作。

public class TestKeyword {

private static final String[] restricted_words_list={"@","of","an","^","#","<",">","(",")"};

private static final Pattern restrictedReplacer;

private static Set<String> restrictedWords = null;

static {

StringBuilder strb= new StringBuilder();

for(String str:restricted_words_list){
strb.append("\\b").append(Pattern.quote(str)).append("\\b|");
}

strb.setLength(strb.length()-1);
restrictedReplacer = Pattern.compile(strb.toString(),Pattern.CASE_INSENSITIVE);

strb = new StringBuilder();
}

public static void main(String[] args)
{
String inputText = "abcd abc@ cbda ssef of jjj t#he g^g an wh&at ggg<g ss%ss ### (()) D^h^D";
System.out.println("inputText : " + inputText);
String modifiedText = restrictedWordCheck(inputText);
System.out.println("Modified Text : " + modifiedText);

}

public static String restrictedWordCheck(String input){
Matcher m = restrictedReplacer.matcher(input);
StringBuffer strb = new StringBuffer(input.length());//ensuring capacity

while(m.find()){
if(restrictedWords==null)restrictedWords = new HashSet<String>();
restrictedWords.add(m.group()); //m.group() returns what was matched
m.appendReplacement(strb,""); //this writes out what came in between matching words

for(int i=m.start();i<m.end();i++)
strb.append("");
}
m.appendTail(strb);
return strb.toString();
}
}

输出是:

输入文本:abcd abc@ cbda ssef of jjj t#he g^g an wh&at ggg

修改文本:abcd abc@ cbda ssef jjj the gg wh&at gggg ss%ss ### (()) DhD

这里排除的单词是ofan,但只是一些特殊字符,而不是我在restricted_words_list中指定的所有字符

<小时/>

现在我有了更好的解决方案:

    String inputText = title;// assigning input 
List<String> restricted_words_list = catalogueService.getWordStopper(); // getting all stopper words from database dynamically (inside getWordStopper() method just i wrote a query and getting list of words)
String finalResult = "";
List<String> stopperCleanText = new ArrayList<String>();

String[] afterTextSplit = inputText.split("\\s"); // split and add to list

for (int i = 0; i < afterTextSplit.length; i++) {
stopperCleanText.add(afterTextSplit[i]); // adding to list
}

stopperCleanText.removeAll(restricted_words_list); // remove all word stopper

for (String addToString : stopperCleanText)
{
finalResult += addToString+";"; // add semicolon to cleaned text
}

return finalResult;

最佳答案

public String replaceAll(String regex,
String replacement)

用给定的替换替换该字符串的每个子字符串(与给定的正则表达式匹配)。

参数:

  • regex - 该字符串要成为的正则表达式匹配
  • replacement - 每个匹配项要替换的字符串。

因此,您只需提供带有空字符串的替换参数即可。

关于java - 如何从输入文本中删除特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20217247/

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