一個可以匹配整數、浮點數的正則表達式


//正則表達式

string regStr =  "^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$";

Description
A regular expression that matches numbers. Integers or decimal numbers with or without the exponential form.
Matches
23 | -17.e23 | +.23e+2
Non-Matches
+.e2 | 23.17.5 | 10e2.0

 

 

 

 

 

驗證方法如下:

View Code
 1 /// <summary>
 2         /// 驗證輸入字符串為數字
 3         /// </summary>
 4         /// <param name="str">輸入字符</param>
 5         /// <returns>返回一個bool類型的值</returns>
 6         private static bool IsNumeric(string str)
 7         {
 8             System.Text.RegularExpressions.Regex reg1
 9                 = new System.Text.RegularExpressions.Regex(@"^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$");
10             if (str != "")
11             {
12                 return reg1.IsMatch(str);
13             }
14             else
15             {
16                 return true;
17             }
18         } 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM