• Unity通过Framwork类库中的Regex类实现了一些特殊功能数据检查


    Unity通过Framwork类库中的Regex类实现了一些特殊功能数据检查

    文章目录

    前言

    提示:个人在写游戏注册登录功能时候需要检测玩家输入内容,比如是否为纯数字或者字母数字组合,是否为邮箱等,此类的功能可以做到简单的检测。


    一、代码类-可直接使用或者自己扩充

    代码如下(示例):

    1. using System;
    2. using System.Text.RegularExpressions;
    3. namespace MetarCommonSupport
    4. {
    5.     /// <summary>
    6.     /// 通过Framwork类库中的Regex类实现了一些特殊功能数据检查
    7.     /// </summary>
    8.     public class MetarnetRegex
    9.     {
    10.         private static MetarnetRegex instance = null;
    11.         public static MetarnetRegex GetInstance()
    12.         {
    13.             if (MetarnetRegex.instance == null)
    14.             {
    15.                 MetarnetRegex.instance = new MetarnetRegex();
    16.             }
    17.             return MetarnetRegex.instance;
    18.         }
    19.         private MetarnetRegex()
    20.         {
    21.         }
    22.         /// <summary>
    23.         /// 判断输入的字符串只包含汉字
    24.         /// </summary>
    25.         /// <param name="input"></param>
    26.         /// <returns></returns>
    27.         public static bool IsChineseCh(string input)
    28.         {
    29.             Regex regex = new Regex("^[\u4e00-\u9fa5]+$");
    30.             return regex.IsMatch(input);
    31.         }
    32.         /// <summary>
    33.         /// 匹配3位或4位区号的电话号码,其中区号可以用小括号括起来,
    34.         /// 也可以不用,区号与本地号间可以用连字号或空格间隔,
    35.         /// 也可以没有间隔
    36.         /// \(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7}|0\d{3}[- ]?\d{7}
    37.         /// </summary>
    38.         /// <param name="input"></param>
    39.         /// <returns></returns>
    40.         public static bool IsPhone(string input)
    41.         {
    42.             string pattern = "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";
    43.             Regex regex = new Regex(pattern);
    44.             return regex.IsMatch(input);
    45.         }
    46.         /// <summary>
    47.         /// 判断输入的字符串是否是一个合法的手机号
    48.         /// </summary>
    49.         /// <param name="input"></param>
    50.         /// <returns></returns>
    51.         public static bool IsMobilePhone(string input)
    52.         {
    53.             Regex regex = new Regex("^13\\d{9}$");
    54.             return regex.IsMatch(input);
    55.         }
    56.         /// <summary>
    57.         /// 判断输入的字符串只包含数字
    58.         /// 可以匹配整数和浮点数
    59.         /// ^-?\d+$|^(-?\d+)(\.\d+)?$
    60.         /// </summary>
    61.         /// <param name="input"></param>
    62.         /// <returns></returns>
    63.         public static bool IsNumber(string input)
    64.         {
    65.             string pattern = "^-?\\d+$|^(-?\\d+)(\\.\\d+)?$";
    66.             Regex regex = new Regex(pattern);
    67.             return regex.IsMatch(input);
    68.         }
    69.         /// <summary>
    70.         /// 匹配非负整数
    71.         ///
    72.         /// </summary>
    73.         /// <param name="input"></param>
    74.         /// <returns></returns>
    75.         public static bool IsNotNagtive(string input)
    76.         {
    77.             Regex regex = new Regex(@"^\d+$");
    78.             return regex.IsMatch(input);
    79.         }
    80.         /// <summary>
    81.         /// 匹配正整数
    82.         /// </summary>
    83.         /// <param name="input"></param>
    84.         /// <returns></returns>
    85.         public static bool IsUint(string input)
    86.         {
    87.             Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
    88.             return regex.IsMatch(input);
    89.         }
    90.         /// <summary>
    91.         /// 判断输入的字符串字包含英文字母
    92.         /// </summary>
    93.         /// <param name="input"></param>
    94.         /// <returns></returns>
    95.         public static bool IsEnglisCh(string input)
    96.         {
    97.             Regex regex = new Regex("^[A-Za-z]+$");
    98.             return regex.IsMatch(input);
    99.         }
    100.         /// <summary>
    101.         /// 判断输入的字符串是否是一个合法的Email地址
    102.         /// </summary>
    103.         /// <param name="input"></param>
    104.         /// <returns></returns>
    105.         public static bool IsEmail(string input)
    106.         {
    107.             string pattern = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
    108.             Regex regex = new Regex(pattern);
    109.             return regex.IsMatch(input);
    110.         }
    111.         /// <summary>
    112.         /// 判断输入的字符串是否只包含数字和英文字母
    113.         /// </summary>
    114.         /// <param name="input"></param>
    115.         /// <returns></returns>
    116.         public static bool IsNumAndEnCh(string input)
    117.         {
    118.             string pattern = @"^[A-Za-z0-9]+$";
    119.             Regex regex = new Regex(pattern);
    120.             return regex.IsMatch(input);
    121.         }
    122.         /// <summary>
    123.         /// 判断输入的字符串是否是一个超链接
    124.         /// </summary>
    125.         /// <param name="input"></param>
    126.         /// <returns></returns>
    127.         public static bool IsURL(string input)
    128.         {
    129.             //string pattern = @"http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?";
    130.             string pattern = @"^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$";
    131.             Regex regex = new Regex(pattern);
    132.             return regex.IsMatch(input);
    133.         }
    134.         /// <summary>
    135.         /// 判断输入的字符串是否是表示一个IP地址
    136.         /// </summary>
    137.         /// <param name="input">被比较的字符串</param>
    138.         /// <returns>是IP地址则为True</returns>
    139.         public static bool IsIPv4(string input)
    140.         {
    141.             string[] IPs = input.Split('.');
    142.             Regex regex = new Regex(@"^\d+$");
    143.             for (int i = 0; i < IPs.Length; i++)
    144.             {
    145.                 if (!regex.IsMatch(IPs[i]))
    146.                 {
    147.                     return false;
    148.                 }
    149.                 if (Convert.ToUInt16(IPs[i]) > 255)
    150.                 {
    151.                     return false;
    152.                 }
    153.             }
    154.             return true;
    155.         }
    156.         /// <summary>
    157.         /// 计算字符串的字符长度,一个汉字字符将被计算为两个字符
    158.         /// </summary>
    159.         /// <param name="input">需要计算的字符串</param>
    160.         /// <returns>返回字符串的长度</returns>
    161.         public static int GetCount(string input)
    162.         {
    163.             return Regex.Replace(input, @"[\u4e00-\u9fa5/g]", "aa").Length;
    164.         }
    165.         /// <summary>
    166.         /// 调用Regex中IsMatch函数实现一般的正则表达式匹配
    167.         /// </summary>
    168.         /// <param name="pattern">要匹配的正则表达式模式。</param>
    169.         /// <param name="input">要搜索匹配项的字符串</param>
    170.         /// <returns>如果正则表达式找到匹配项,则为 true;否则,为 false。</returns>
    171.         public static bool IsMatch(string pattern, string input)
    172.         {
    173.             Regex regex = new Regex(pattern);
    174.             return regex.IsMatch(input);
    175.         }
    176.         /// <summary>
    177.         /// 从输入字符串中的第一个字符开始,用替换字符串替换指定的正则表达式模式的所有匹配项。
    178.         /// </summary>
    179.         /// <param name="pattern">模式字符串</param>
    180.         /// <param name="input">输入字符串</param>
    181.         /// <param name="replacement">用于替换的字符串</param>
    182.         /// <returns>返回被替换后的结果</returns>
    183.         public static string Replace(string pattern, string input, string replacement)
    184.         {
    185.             Regex regex = new Regex(pattern);
    186.             return regex.Replace(input, replacement);
    187.         }
    188.         /// <summary>
    189.         /// 在由正则表达式模式定义的位置拆分输入字符串。
    190.         /// </summary>
    191.         /// <param name="pattern">模式字符串</param>
    192.         /// <param name="input">输入字符串</param>
    193.         /// <returns></returns>
    194.         public static string[] Split(string pattern, string input)
    195.         {
    196.             Regex regex = new Regex(pattern);
    197.             return regex.Split(input);
    198.         }
    199.         /// <summary>
    200.         /// 判断输入的字符串是否是合法的IPV6 地址
    201.         /// </summary>
    202.         /// <param name="input"></param>
    203.         /// <returns></returns>
    204.         public static bool IsIPV6(string input)
    205.         {
    206.             string pattern = "";
    207.             string temp = input;
    208.             string[] strs = temp.Split(':');
    209.             if (strs.Length > 8)
    210.             {
    211.                 return false;
    212.             }
    213.             int count = MetarnetRegex.GetStringCount(input, "::");
    214.             if (count > 1)
    215.             {
    216.                 return false;
    217.             }
    218.             else if (count == 0)
    219.             {
    220.                 pattern = @"^([\da-f]{1,4}:){7}[\da-f]{1,4}$";
    221.                 Regex regex = new Regex(pattern);
    222.                 return regex.IsMatch(input);
    223.             }
    224.             else
    225.             {
    226.                 pattern = @"^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$";
    227.                 Regex regex1 = new Regex(pattern);
    228.                 return regex1.IsMatch(input);
    229.             }
    230.         }
    231.         /* *******************************************************************
    232.         * 1、通过“:”来分割字符串看得到的字符串数组长度是否小于等于8
    233.         * 2、判断输入的IPV6字符串中是否有“::”。
    234.         * 3、如果没有“::”采用 ^([\da-f]{1,4}:){7}[\da-f]{1,4}$ 来判断
    235.         * 4、如果有“::” ,判断"::"是否止出现一次
    236.         * 5、如果出现一次以上 返回false
    237.         * 6、^([\da-f]{1,4}:){0,5}::([\da-f]{1,4}:){0,5}[\da-f]{1,4}$
    238.         * ******************************************************************/
    239.         /// <summary>
    240.         /// 判断字符串compare 在 input字符串中出现的次数
    241.         /// </summary>
    242.         /// <param name="input">源字符串</param>
    243.         /// <param name="compare">用于比较的字符串</param>
    244.         /// <returns>字符串compare 在 input字符串中出现的次数</returns>
    245.         private static int GetStringCount(string input, string compare)
    246.         {
    247.             int index = input.IndexOf(compare);
    248.             if (index != -1)
    249.             {
    250.                 return 1 + GetStringCount(input.Substring(index + compare.Length), compare);
    251.             }
    252.             else
    253.             {
    254.                 return 0;
    255.             }
    256.         }
    257.     }
    258. }


  • 相关阅读:
    【OpenSSL】单向散列函数
    oslo_rootwrap学习小结
    【大数据分析】基于Graphx的shortestpath源码分析
    About Stacked Generalization
    Spring拦截器的简单应用
    docker 容器环境配置 mydumper
    Android 13.0 SystemUI状态栏屏蔽掉通知栏不显示通知
    如何在Spring Boot中如何返回InputStream时采用文件的模式呢?
    【Python】基础语法1(常量与表达式、变量和类型、注释、输入输出、运算符)
    Pulsar Meetup 深圳 2024 会务介绍
  • 原文地址:https://blog.csdn.net/leo347/article/details/125513572