正则表达式判断字符串是否为数字[转]
1using System;
2using System.Text.RegularExpressions;
3
4public bool IsNumber(String strNumber)
5{
6 Regex objNotNumberPattern=new Regex("[^0-9.-]");
7 Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
8 Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
9 String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
10 String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
11 Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
12 return !objNotNumberPattern.IsMatch(strNumber) &&
13 !objTwoDotPattern.IsMatch(strNumber) &&
14 !objTwoMinusPattern.IsMatch(strNumber) &&
15 objNumberPattern.IsMatch(strNumber);
16}
17public static bool IsNumeric(string value)
18{
19 return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
20}
21public static bool IsInt(string value)
22{
23 return Regex.IsMatch(value, @"^[+-]?\d*$");
24}
25public static bool IsUnsign(string value)
26{
27 return Regex.IsMatch(value, @"^\d*[.]?\d*$");
28}
29
2using System.Text.RegularExpressions;
3
4public bool IsNumber(String strNumber)
5{
6 Regex objNotNumberPattern=new Regex("[^0-9.-]");
7 Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
8 Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
9 String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
10 String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
11 Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
12 return !objNotNumberPattern.IsMatch(strNumber) &&
13 !objTwoDotPattern.IsMatch(strNumber) &&
14 !objTwoMinusPattern.IsMatch(strNumber) &&
15 objNumberPattern.IsMatch(strNumber);
16}
17public static bool IsNumeric(string value)
18{
19 return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
20}
21public static bool IsInt(string value)
22{
23 return Regex.IsMatch(value, @"^[+-]?\d*$");
24}
25public static bool IsUnsign(string value)
26{
27 return Regex.IsMatch(value, @"^\d*[.]?\d*$");
28}
29
posted on 2008-04-08 17:57 john.huang 阅读(1146) 评论(0) 编辑 收藏 举报