• 关键字加链接


    private static readonly Regex reg_b = new Regex(@"\B", RegexOptions.Compiled);
    private static readonly Regex reg_en = new Regex(@"[a-zA-Z]+", RegexOptions.Compiled);
    private static readonly Regex reg_num = new Regex(@"^[\-\.\s\d]+$", RegexOptions.Compiled);
     
    private static Regex reg_word = null//组合所有屏蔽词的正则
     
    private static Regex GetRegex()
    {
        if (reg_word == null)
        {
            reg_word = new Regex(GetPattern(), RegexOptions.Compiled | RegexOptions.IgnoreCase);
        }
        return reg_word;
    }
     
    /// <summary>
    /// 检查输入内容是否包含脏词(包含返回true)
    /// </summary>
    public static bool HasBlockWords(string raw)
    {
        return GetRegex().Match(raw).Success;
    }
    /// <summary>
    /// 脏词替换成*号
    /// </summary>
    public static string WordsFilter(string raw)
    {
        return GetRegex().Replace(raw, "***");
    }
    /// <summary>
    /// 获取内容中含有的脏词
    /// </summary>
    public static IEnumerable<string> GetBlockWords(string raw)
    {
        foreach (Match mat in reg_word.Matches(raw))
        {
            yield return (mat.Value);
        }
    }
    private static string GetPattern()
    {
        StringBuilder patt = new StringBuilder();
        string s;
        foreach (string word in GetBlockWords())
        {
            if (word.Length == 0continue;
            if (word.Length == 1)
            {
                patt.AppendFormat("|({0})", word);
            }
            else if (reg_num.IsMatch(word))
            {
                patt.AppendFormat("|({0})", word);
            }
            else if (reg_en.IsMatch(word))
            {
                s = reg_b.Replace(word, @"(?:[^a-zA-Z]{0,3})");
                patt.AppendFormat("|({0})", s);
            }
            else
            {
                s = reg_b.Replace(word, @"(?:[^\u4e00-\u9fa5]{0,3})");
                patt.AppendFormat("|({0})", s);
            }
        }
        if (patt.Length > 0)
        {
            patt.Remove(01);
        }
        return patt.ToString();
    }
     
    /// <summary>
    /// 获取所有脏词
    /// </summary>
    public static string[] GetBlockWords()
    {
        return new string[]{"国民党","fuck","110"};//这里应该从数据库获取
    }
  • 相关阅读:
    windows10下Mysql5.7安装指南
    SVN迁移Gitlab步骤
    java.lang.String中的replace方法到底替换了一个还是全部替换了。
    MongoDB升级导致启动失败
    阿里云CentOS7.3搭建多用户私有git服务器(从安装git开始)
    CentOS7.3编译安装Nginx设置开机启动
    关于使用Xshell远程连接启动tomcat导致图片不显示,报错Could not initialize class sun.awt.X11GraphicsEnvironment解决方案
    easyui报错“Cannot read poperty 'options' of undefined”问题解决方案之一
    Ubuntu18.04系统中vi键盘输入字符不匹配
    ux.form.field.TreePicker 扩展,修复火狐不能展开bug
  • 原文地址:https://www.cnblogs.com/bober/p/2726759.html
Copyright © 2020-2023  润新知