在java中使用正则表达式检查单词

时间:2018-03-17 10:14:58

标签: java regex

我有来自客户的输入消息:

"This is example message with bad words like FWORD"

我有模式允许我检查此消息,但我想要输出如下:

"This is example message with bad words like F***D"

我只想用开头替换所有字母,除了第一个和最后一个字符。我怎么能这样做?

提前致谢。

4 个答案:

答案 0 :(得分:1)

也许你可以用空格分割字符串,循环单词和每个单词检查它是否等于被删除的单词。 如果是,请使用替换为正则表达式:

\\B\\w\\B

那就匹配

  • \B断言\b不匹配的位置
  • \w单词字符(如果您想要匹配更多内容,请更新您的要求)
  • \B断言\b不匹配的位置

举个例子:

String censored = "FWORD";
String str = "This is example message with bad words like FWORD ";        
String[] words = str.split(" "); 

for (int i = 0; i < words.length; i++) {
    if (words[i].equals(censored)) {
            words[i] = words[i].replaceAll("\\B\\w\\B", "*");         
        }
    }    
System.out.println(String.join(" ", words));

那会给你

  

这是带有错误字样的示例消息,如F *** D

Demo Java

如果您将censored更改为“消息”,则会产生:

  

这是带有像FWORD

这样的坏词的电子邮件

如果你想列出多个坏词,你可以创建一个包含坏词的数组并循环遍历该数组,检查每个单词如this Demo

你的结果可能是这样的:

  

这是带有F *** D

之类错误字样的电子邮件

答案 1 :(得分:0)

String有一个名为.replaceAll的方法(String regex,String replacement)。 你可以简单地调用.replaceAll来查找你想要审查的单词。 例如:

    stringToEdit.replaceAll("FWORD","F***D");

如果你想要审查多个单词,我建议保留多个正则表达式,一个用于Map中的每个单词,其中键是正则表达式,值是该单词的删失版本,或者另一个四处走走。然后,您可以浏览每个映射条目并在字符串上使用replaceAll。

    public static void main (String args[]){

        String toCensor="you can add the dirty words here";

        String censorRegex1="([cC]?)+([hH])+[uU]+[jJ]+([uU+]?)";
        String censorRegex2="[kK]+[Uu]+[rR]+[wW]+[aA]+";

        Map<String, String> censorMap=new HashMap<String, String>();
        censorMap.put(censorRegex1,"c***u");
        censorMap.put(censorRegex2,"k***a");

        for(Map.Entry<String,String> e:censorMap.entrySet()){
            toCensor=toCensor.replaceAll(e.getKey(),e.getValue());
        }

        System.out.println(toCensor);

    }

答案 2 :(得分:0)

我建议使用正则表达式,因为它可以编译一次,使得有效的有限状态机能够在一次传递中进行替换。

String censored_re_str = "(?i)(b)adwor(d)|(v)erybadwor(d)|(y)etmorebadwor(d)";
Pattern censored = Pattern.compile(censored_re_str);

censored.matcher(string_to_replace).replaceAll("$1***$2");

答案 3 :(得分:0)

试试这个(使用apache.commons.lang3 lib)

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

public class FilterApp {

    public static void main(String[] args) {
        // Define your filter
        String[] filterArray = new String[]{"FWORD"};
        // Build a Map with replacements
        Map<String,String> filterMap = new HashMap<>();
        for( String f: filterArray){
            String replacement = f.charAt(0)+StringUtils.leftPad("*", (f.length()-2), '*')+f.charAt(f.length()-1);
            // (?i) means 'CASE_INSENSITIVE' and \\b means 'word boundry'
            filterMap.put("(?i)\\b"+f+"\\b", replacement);
        }
        // Let's do it....
        String result = "This is example message with bad words like FWORD";
        for (Map.Entry<String, String> filter : filterMap.entrySet()) {
            result = result.replaceAll(filter.getKey(), filter.getValue());
        }
        // .. and here is the result
        System.out.println(result);
    }
}