Regex to remove blacklisted words from a sentence

前端 未结 3 1186
野趣味
野趣味 2021-01-21 01:34

How can I use a regext to filter out a list of blacklisted(Obscene) words, such that if a black listed words is like \'Bill Joseph\'

 Then \'I am Bill Josephine         


        
相关标签:
3条回答
  • 2021-01-21 01:56

    Make sure the word is surrounded by a word boundary ".*\\b" + badWord + "\\b.*"

    0 讨论(0)
  • 2021-01-21 02:02

    Simple, and this works:

    String badStrRegex = "\\WBill Joseph\\W?";
    Pattern pattern = Pattern.compile(badStrRegex);
    Matcher m = pattern.matcher(testStr);  //testStr is your string under test
    boolean isBad = m.find();
    

    It works!! Tested against all your input.

    0 讨论(0)
  • 2021-01-21 02:02

    Use the negation of the alphanumeric character class:

    "[^A-Za-z0-9]Bill Joseph[^A-Za-z0-9]"

    Using "\W" in place of "[^A-Za-z0-9]" would work in most cases except when there is an underscore before/after the name. So "Bill Joseph_" still would be seen as valid.

    0 讨论(0)
提交回复
热议问题