C#中幾個正規表示式匹配輸入字元的函式
以下幾個函式主要是利用正規表示式格式化使用者輸入的不當資料
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public static int ParseInt32(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
return int.Parse(value);
}
}
public static float ParseFloat(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0f;
else
{
return float.Parse(value);
}
}
///
/// 功能:將字元轉換為單精度型,正規表示式匹配速度比較慢,慎用。
/// 建議在外部輸入的資料(可能是不安全的資料)時使用
/// 作者:胡進平
/// 日期:2008-9-12
///
/// 要轉換的字元
/// 是否需要嚴格判斷
///
public static float ParseFloat(string value,bool isStrict)
{
if (!isStrict) return ParseFloat(value);
if (string.IsNullOrEmpty(value.Trim()))
return 0f;
else
{
Regex rx = new Regex(@"(-?\d+\.\d*)|(-?\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToSingle(matches[0].Value);
}
return 0f;
}
}
///
/// 功能:將字元轉換為整型,正規表示式匹配速度比較慢,慎用
/// 建議在外部輸入的資料(可能是不安全的資料)時使用
/// 作者:胡進平
/// 日期:2008-9-12
///
/// 要轉換的字元
/// 是否需要嚴格判斷
///
public static int ParseInt32(string value,bool isStrict)
{
if (!isStrict) return ParseInt32(value);
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
Regex rx = new Regex(@"-?\d+\.\d*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
string prefix = matches[0].Value.Split('.')[0];
return Convert.ToInt32(prefix);
}
rx = new Regex(@"-?\d+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToInt32(matches[0].Value);
}
return 0;
}
}
///
/// 功能:將字元轉換為雙精度型
/// 作者:胡進平
/// 日期:2008-9-12
///
///
///
public static double ParseDobule(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
Regex rx = new Regex(@"(-?\d+\.\d*)|(-?\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToDouble(matches[0].Value);
}
return 0;
}
}public static int ParseInt32(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
return int.Parse(value);
}
}
public static float ParseFloat(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0f;
else
{
return float.Parse(value);
}
}
///
/// 功能:將字元轉換為單精度型,正規表示式匹配速度比較慢,慎用。
/// 建議在外部輸入的資料(可能是不安全的資料)時使用
/// 作者:胡進平
/// 日期:2008-9-12
///
/// 要轉換的字元
/// 是否需要嚴格判斷
///
public static float ParseFloat(string value,bool isStrict)
{
if (!isStrict) return ParseFloat(value);
if (string.IsNullOrEmpty(value.Trim()))
return 0f;
else
{
Regex rx = new Regex(@"(-?\d+\.\d*)|(-?\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToSingle(matches[0].Value);
}
return 0f;
}
}
///
/// 功能:將字元轉換為整型,正規表示式匹配速度比較慢,慎用
/// 建議在外部輸入的資料(可能是不安全的資料)時使用
/// 作者:胡進平
/// 日期:2008-9-12
///
/// 要轉換的字元
/// 是否需要嚴格判斷
///
public static int ParseInt32(string value,bool isStrict)
{
if (!isStrict) return ParseInt32(value);
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
Regex rx = new Regex(@"-?\d+\.\d*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
string prefix = matches[0].Value.Split('.')[0];
return Convert.ToInt32(prefix);
}
rx = new Regex(@"-?\d+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToInt32(matches[0].Value);
}
return 0;
}
}
///
/// 功能:將字元轉換為雙精度型
/// 作者:胡進平
/// 日期:2008-9-12
///
///
///
public static double ParseDobule(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
Regex rx = new Regex(@"(-?\d+\.\d*)|(-?\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToDouble(matches[0].Value);
}
return 0;
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->public static int ParseInt32(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
return int.Parse(value);
}
}
public static float ParseFloat(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0f;
else
{
return float.Parse(value);
}
}
///
/// 功能:將字元轉換為單精度型,正規表示式匹配速度比較慢,慎用。
/// 建議在外部輸入的資料(可能是不安全的資料)時使用
/// 作者:胡進平
/// 日期:2008-9-12
///
/// 要轉換的字元
/// 是否需要嚴格判斷
///
public static float ParseFloat(string value,bool isStrict)
{
if (!isStrict) return ParseFloat(value);
if (string.IsNullOrEmpty(value.Trim()))
return 0f;
else
{
Regex rx = new Regex(@"(-?\d+\.\d*)|(-?\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToSingle(matches[0].Value);
}
return 0f;
}
}
///
/// 功能:將字元轉換為整型,正規表示式匹配速度比較慢,慎用
/// 建議在外部輸入的資料(可能是不安全的資料)時使用
/// 作者:胡進平
/// 日期:2008-9-12
///
/// 要轉換的字元
/// 是否需要嚴格判斷
///
public static int ParseInt32(string value,bool isStrict)
{
if (!isStrict) return ParseInt32(value);
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
Regex rx = new Regex(@"-?\d+\.\d*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
string prefix = matches[0].Value.Split('.')[0];
return Convert.ToInt32(prefix);
}
rx = new Regex(@"-?\d+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToInt32(matches[0].Value);
}
return 0;
}
}
///
/// 功能:將字元轉換為雙精度型
/// 作者:胡進平
/// 日期:2008-9-12
///
///
///
public static double ParseDobule(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
Regex rx = new Regex(@"(-?\d+\.\d*)|(-?\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToDouble(matches[0].Value);
}
return 0;
}
}public static int ParseInt32(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
return int.Parse(value);
}
}
public static float ParseFloat(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0f;
else
{
return float.Parse(value);
}
}
///
/// 功能:將字元轉換為單精度型,正規表示式匹配速度比較慢,慎用。
/// 建議在外部輸入的資料(可能是不安全的資料)時使用
/// 作者:胡進平
/// 日期:2008-9-12
///
/// 要轉換的字元
/// 是否需要嚴格判斷
///
public static float ParseFloat(string value,bool isStrict)
{
if (!isStrict) return ParseFloat(value);
if (string.IsNullOrEmpty(value.Trim()))
return 0f;
else
{
Regex rx = new Regex(@"(-?\d+\.\d*)|(-?\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToSingle(matches[0].Value);
}
return 0f;
}
}
///
/// 功能:將字元轉換為整型,正規表示式匹配速度比較慢,慎用
/// 建議在外部輸入的資料(可能是不安全的資料)時使用
/// 作者:胡進平
/// 日期:2008-9-12
///
/// 要轉換的字元
/// 是否需要嚴格判斷
///
public static int ParseInt32(string value,bool isStrict)
{
if (!isStrict) return ParseInt32(value);
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
Regex rx = new Regex(@"-?\d+\.\d*", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
string prefix = matches[0].Value.Split('.')[0];
return Convert.ToInt32(prefix);
}
rx = new Regex(@"-?\d+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToInt32(matches[0].Value);
}
return 0;
}
}
///
/// 功能:將字元轉換為雙精度型
/// 作者:胡進平
/// 日期:2008-9-12
///
///
///
public static double ParseDobule(string value)
{
if (string.IsNullOrEmpty(value.Trim()))
return 0;
else
{
Regex rx = new Regex(@"(-?\d+\.\d*)|(-?\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (rx.IsMatch(value))
{
MatchCollection matches = rx.Matches(value);
return Convert.ToDouble(matches[0].Value);
}
return 0;
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-462838/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 匹配空白字元正規表示式字元
- 正規表示式匹配雙位元組字元字元
- 正規表示式教程之匹配單個字元詳解字元
- 匹配純英文字元正規表示式字元
- 正規表示式匹配
- 正規表示式實現字元的模糊匹配功能示例字元
- 正規表示式的多行匹配
- 正規表示式匹配原理
- 字串——正規表示式匹配字串
- python 正規表示式匹配Python
- 正規表示式 多行匹配
- 匹配字母正規表示式
- leetcode - 正規表示式匹配LeetCode
- 正規表示式 ^元字元字元
- 正規表示式 $ 元字元字元
- 正規表示式教程之匹配一組字元詳解字元
- 正規表示式匹配html中的圖片HTML
- 正規表示式 字元和字元類字元
- 匹配正整數正規表示式
- 正規表示式中括號[]字元類字元
- 正規表示式 中括號[] 字元類字元
- 轉義正規表示式中特殊字元字元
- 正規表示式支配匹配模式模式
- JavaScript匹配中文正規表示式JavaScript
- 匹配護照正規表示式
- 匹配小數正規表示式
- 匹配負數正規表示式
- 匹配整數正規表示式
- 匹配自然數正規表示式
- 匹配航班號正規表示式
- 正規表示式匹配漢字
- 10. 正規表示式匹配
- 正規表示式匹配問題
- 匹配字母、數字和中文字元正規表示式字元
- 正規表示式 轉義字元字元
- 正規表示式 \d元字元字元
- 正規表示式 \t 元字元字元
- 正規表示式 \t元字元字元
- 正規表示式 \0元字元字元