c# - Why doesn't my chat filter check words correctly? -
i'm trying make kind of chat filter. wanna check "wo rd"
or "w ord"
or "w.ord"
. if enter "word wo rd" , outputs "**** **** ****"
(should "**** ****"
). also, if disable dot
checking, ignores dots..
here code:
//rextester.program.main entry point code. don't change it. //compiler version 4.0.30319.17929 microsoft (r) .net framework 4.5 using system; using system.collections.generic; using system.linq; using system.text.regularexpressions; namespace rextester { public class program { public static void main(string[] args) { notallowedwords.add(new worddata("word", true, false)); var input = console.readline(); var splitted = input.split(char.parse(" ")); foreach (string s in splitted) { var index = findword(input,s); if (index != -1) { input = input.replace(s, "****"); } } console.writeline(input); } private static int findword (string input, string word) { var splitted = input.split(char.parse(" ")); bool next = false; foreach (var w in splitted) { foreach (var in notallowedwords) { if (a.compareto(word)) return input.indexof(word); } if (next) { next = false; if (w.substring(0, word.length - 1) == word.substring(1)) return input.indexof(w); } if (w.last() == word[0]) next = true; } return -1; } public static list<worddata> notallowedwords = new list<worddata>(); } public class worddata { protected worddata() { ignorewhitespace = true; } public bool compareto(string word) { var input = word; if (this.notallowdots) { input = input.replace(".",""); } if (this.ignorewhitespace) { input = input.trim(); } if (input.equals(input, stringcomparison.currentcultureignorecase) && !this.allowedversions.contains(input)) return true; return false; } public worddata(string word, bool ignorewhitespace = true, bool notallowdots = true, params string[] allowedversions) : base() { this.allowedversions = allowedversions.tolist(); this.ignorewhitespace = ignorewhitespace; this.notallowdots = notallowdots; this.word = word; } public string word { get; set;} private list<string> allowedversions = new list<string>(); public list<string> allowedversions { { return allowedversions; } } public bool ignorewhitespace { get; set;} public bool notallowdots {get; set;} } }
input : word wo.rd wo rd
output : **** **** **** ****
expected output : **** word ****
here how regex . can use online tool test different patterns http://regexstorm.net/tester
class program { static void main(string[] args) { var notallowedword= new notallowedword("word"); var input = "replace word wo.rd wor d , wor.d not wordpress ."; var replacedtext = notallowedword.replacenotallowedword(input); console.writeline(replacedtext); //output //replace **** ****also ****and ****but not wordpress . console.readline(); } public class notallowedword { private readonly string _word; private readonly regex _wordregex; private readonly string _replacement; public notallowedword(string word) { _word = word; _replacement=new string('*',word.length); _wordregex = buildregexpattern(); } private regex buildregexpattern() { //todo use string builder var pattern = ""; //create space , . match pattern // example given "word" produce "w[ ]*\.*o[ ]*\.*r[ ]*\.*l[ ]*\.*d" foreach (var c in _word) { pattern = pattern + c + @"[ ]*\.*"; } var regex = $"({_word}[ ])" + //match word $"|({pattern}[ ])" + //match word contains space or . $"|({pattern}$)" //match word @ end of sentence ; //console.writeline(regex); return new regex(regex); } public string replacenotallowedword(string inputtext) { return _wordregex.replace(inputtext, _replacement); } } }
Comments
Post a Comment