c#中一些简单的正则表达式应用

 

代码
public static bool IsNumber(string inString)
        {
            
if (string.IsNullOrEmpty(inString)) return false;
            Regex regex 
= new Regex(@"^\d*$");
            
return regex.IsMatch(inString.Trim());
        }
        
public static bool IsIntToInt32(string inString, ref Int32 Value)
        {
            
if (string.IsNullOrEmpty(inString)) return false;
            
if (inString.Trim() == "0") { Value = 0return true; }
            Regex regex 
= new Regex(@"^-?[1-9]\d*$");

            
bool tmpBool = regex.IsMatch(inString.Trim());
            
if (tmpBool)
                Value 
= Int32.Parse(inString);
            
return tmpBool;
        }
        
public static bool IsIntToInt16(string inString, ref Int16 Value)
        {
            
if (string.IsNullOrEmpty(inString)) return false;
            
if (inString.Trim() == "0") { Value = 0return true; }
            Regex regex 
= new Regex(@"^-?[1-9]\d*$");

            
bool tmpBool = regex.IsMatch(inString.Trim());
            
if (tmpBool)
                Value 
= Int16.Parse(inString);
            
return tmpBool;
        }
        
public static bool IsUIntToInt32(string inString, ref UInt32 Value)
        {
            
if (string.IsNullOrEmpty(inString)) return false;
            
if (inString.Trim() == "0") { Value = 0return true; }
            Regex regex 
= new Regex(@"^[1-9]\d*$");

            
bool tmpBool = regex.IsMatch(inString.Trim());
            
if (tmpBool)
                Value 
= UInt32.Parse(inString);
            
return tmpBool;
        }
        
public static bool IsUIntToInt16(string inString, ref UInt16 Value)
        {
            
if (string.IsNullOrEmpty(inString)) return false;
            
if (inString.Trim() == "0") { Value = 0return true; }
            Regex regex 
= new Regex(@"^[1-9]\d*$");

            
bool tmpBool = regex.IsMatch(inString.Trim());
            
if (tmpBool)
                Value 
= UInt16.Parse(inString);
            
return tmpBool;
        }

        
public static bool IsFloatToDouble(string inString, ref Double Value)
        {
            
if (string.IsNullOrEmpty(inString)) return false;
            
if (inString.Trim() == "0") { Value = 0return true; }
            Regex regex 
= new Regex(@"^-?([1-9]\d*.\d+|[1-9]\d*|0.\d*[1-9]+|0.\d*[0]+|0)$");

            
bool tmpBool = regex.IsMatch(inString.Trim());
            
if (tmpBool)
                Value 
= Double.Parse(inString);
            
return tmpBool;
        }
        
public static bool IsFloatToSingle(string inString, ref Single Value)
        {
            
if (string.IsNullOrEmpty(inString)) return false;
            
if (inString.Trim() == "0") { Value = 0return true; }
            Regex regex 
= new Regex(@"^-?([1-9]\d*.\d+|[1-9]\d*|0.\d*[1-9]+|0.\d*[0]+|0)$");

            
bool tmpBool = regex.IsMatch(inString.Trim());
            
if (tmpBool)
                Value 
= Single.Parse(inString);
            
return tmpBool;
        }

 

 

posted @ 2010-07-01 12:21  风叙  阅读(121)  评论(0)    收藏  举报