1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System.Text.RegularExpressions;
- namespace MainForm
- {
- /// <summary>
- /// 判断字符串格式是否合法类
- /// </summary>
- class IsStrLegal
- {
- /// <summary>
- /// 判断字符串是否只包含汉字
- /// </summary>
- /// <param name="input">字符串</param>
- /// <returns></returns>
- public static bool IsChineseCh(string input)
- {
- string pattern = @"^[\u4E00-\u9FA5]+$";
- Regex regex = new Regex(pattern);
- return regex.IsMatch(input);
- }
- /// <summary>
- /// 判断字符串是否只包含英文字母
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static bool IsEnglisCh(string input)
- {
- string pattern = @"^[A-Z]|[a-z]+$";
- Regex regex = new Regex(pattern);
- return regex.IsMatch(input);
- }
- /// <summary>
- /// 判断字符串是否只包含整数或浮点数
- /// </summary>
- /// <param name="input">字符串</param>
- /// <returns></returns>
- public static bool IsNumber(string input)
- {
- string pattern = @"^-?\d+$|^(-?\d+)(\.\d+)?$";
- Regex regex = new Regex(pattern);
- return regex.IsMatch(input);
- }
- /// <summary>
- /// 判断字符串是否只包含正整数或正浮点数
- /// </summary>
- /// <param name="input">字符串</param>
- /// <returns></returns>
- public static bool IsUNumber(string input)
- {
- string pattern = @"^([0-9]\d*)(\.\d+)?$";
- Regex regex = new Regex(pattern);
- return regex.IsMatch(input);
- }
- /// <summary>
- /// 判断字符串是否只包含整数
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static bool IsInteger(string input)
- {
- string pattern = @"^-?\d+$|^(-?\d+)?$";
- Regex regex = new Regex(pattern);
- return regex.IsMatch(input);
- }
- /// <summary>
- /// 判断字符串是否只包含正整数
- /// </summary>
- /// <param name="input">字符串</param>
- /// <returns></returns>
- public static bool IsUInteger(string input)
- {
- string pattern = @"^\d+$";
- Regex regex = new Regex(pattern);
- return regex.IsMatch(input);
- }
- /// <summary>
- /// 是否为合法的XML元素节点名称
- /// </summary>
- /// <param name="pointName">点位名称</param>
- /// <returns></returns>
- public static bool IsNodeName(string pointName)
- {
- string RegexStr1 = @"[a-z]|[A-Z]|[_]|[\u4E00-\u9FA5]"; //名称的开头必须是字母、汉字或下划线“_”;
- string RegexStr2 = @"^[\w-.]+$"; //名称的字符串只能包含字母,数字,下划线“_”,“-”,“.”;
- bool result1 = Regex.IsMatch(pointName.Substring(0, 1), RegexStr1);//判断名称的开头
- bool result2 = Regex.IsMatch(pointName, RegexStr2); //判断名称整体
- if (result1 && result2) return true;
- else return false;
- }
- }
- }
|