using System.Text.RegularExpressions;
namespace MainForm
{
///
/// 判断字符串格式是否合法类
///
class IsStrLegal
{
///
/// 判断字符串是否只包含汉字
///
/// 字符串
///
public static bool IsChineseCh(string input)
{
string pattern = @"^[\u4E00-\u9FA5]+$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
///
/// 判断字符串是否只包含英文字母
///
///
///
public static bool IsEnglisCh(string input)
{
string pattern = @"^[A-Z]|[a-z]+$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
///
/// 判断字符串是否只包含整数或浮点数
///
/// 字符串
///
public static bool IsNumber(string input)
{
string pattern = @"^-?\d+$|^(-?\d+)(\.\d+)?$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
///
/// 判断字符串是否只包含正整数或正浮点数
///
/// 字符串
///
public static bool IsUNumber(string input)
{
string pattern = @"^([0-9]\d*)(\.\d+)?$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
///
/// 判断字符串是否只包含整数
///
///
///
public static bool IsInteger(string input)
{
string pattern = @"^-?\d+$|^(-?\d+)?$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
///
/// 判断字符串是否只包含正整数
///
/// 字符串
///
public static bool IsUInteger(string input)
{
string pattern = @"^\d+$";
Regex regex = new Regex(pattern);
return regex.IsMatch(input);
}
///
/// 是否为合法的XML元素节点名称
///
/// 点位名称
///
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;
}
}
}