public List <BadWordInfo> BadWordsDetails(string input) { if (input != null && OnlyAlphaRegex.IsMatch(input)) { List <BadWordInfo> indexList = new List <BadWordInfo>(); foreach (var lang in Enum.GetValues(typeof(LanguageEnum)).Cast <LanguageEnum>()) { if (BadRegexData.ContainsKey(lang)) { var regexInfo = BadRegexData[lang]; if (regexInfo.IsActive) { regexInfo.Regex.ForEach(badRegex => { var badMatch = badRegex.Match(input); while (badMatch.Success) { indexList.Add(new BadWordInfo(badMatch.Index, badMatch.Length)); badMatch = badMatch.NextMatch(); } }); } } } return(indexList.Any() ? indexList : null); } return(null); }
private void LoadBadList(LanguageEnum language) { if (!BadRegexData.ContainsKey(language)) { TextReader tr = File.OpenText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Data", $"bad.{(int)language}.txt")); var badList = tr.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList(); BadRegexData.Add(language, new RegexInfo() { IsActive = false, Regex = badList.Select(bad => new Regex(@"\b" + bad + @"\b", RegexOptions.IgnoreCase | RegexOptions.Compiled)).ToList() }); } }