回答
^[0-9]([.,][0-9]{1,3})?$
它允許:
0
1
1.2
1.02
1.003
1.030
1,2
1,23
1,234
BUT NOT:
.1
,1
12.1
12,1
1.
1,
1.2345
1,2345
用\ d替換[0-9]可以刪除3個字符(共6個)並執行相同的操作。 – UnkwnTech 2009-06-09 08:30:01
+1,但接受.1是不是很好,因爲這是0.1的有效表示(用Decimal.Parse測試)? – stevehipwell 2009-06-09 08:34:18
這個最簡單的版本是「\ d(\。\ d {1,3})?」但使用「\ d(?:\。\ d {1,3})?」將意味着一個組不被存儲。 – stevehipwell 2009-06-09 08:39:15
\d{1}(\.\d{1,3})?
Match a single digit 0..9 «\d{1}»
Exactly 1 times «{1}»
Match the regular expression below and capture its match into backreference number 1 «(\.\d{1,3})?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the character 「.」 literally «\.»
Match a single digit 0..9 «\d{1,3}»
Between one and 3 times, as many times as possible, giving back as needed (greedy) «{1,3}»
Created with RegexBuddy
相配:
1.2
1.23
1 .234
還有一種替代方法,它沒有I18n問題(允許','或'。',但不能同時存在):Decimal.TryParse
。
只要嘗試轉換,忽略該值。
bool IsDecimalFormat(string input) {
Decimal dummy;
return Decimal.TryParse(input, out dummy);
}
這比使用正則表達式快得多,見下文。
(的Decimal.TryParse
過載可被用於更好的控制。)
性能測試結果:Decimal.TryParse:0.10277ms,正則表達式:0.49143ms
代碼(PerformanceHelper.Run
是一個輔助比對於運行通過迭代次數委託並返回平均TimeSpan
):
using System;
using System.Text.RegularExpressions;
using DotNetUtils.Diagnostics;
class Program {
static private readonly string[] TestData = new string[] {
"10.0",
"10,0",
"0.1",
".1",
"Snafu",
new string('x', 10000),
new string('2', 10000),
new string('0', 10000)
};
static void Main(string[] args) {
Action parser =() => {
int n = TestData.Length;
int count = 0;
for (int i = 0; i < n; ++i) {
decimal dummy;
count += Decimal.TryParse(TestData[i], out dummy) ? 1 : 0;
}
};
Regex decimalRegex = new Regex(@"^[0-9]([\.\,][0-9]{1,3})?$");
Action regex =() => {
int n = TestData.Length;
int count = 0;
for (int i = 0; i < n; ++i) {
count += decimalRegex.IsMatch(TestData[i]) ? 1 : 0;
}
};
var paserTotal = 0.0;
var regexTotal = 0.0;
var runCount = 10;
for (int run = 1; run <= runCount; ++run) {
var parserTime = PerformanceHelper.Run(10000, parser);
var regexTime = PerformanceHelper.Run(10000, regex);
Console.WriteLine("Run #{2}: Decimal.TryParse: {0}ms, Regex: {1}ms",
parserTime.TotalMilliseconds,
regexTime.TotalMilliseconds,
run);
paserTotal += parserTime.TotalMilliseconds;
regexTotal += regexTime.TotalMilliseconds;
}
Console.WriteLine("Overall averages: Decimal.TryParse: {0}ms, Regex: {1}ms",
paserTotal/runCount,
regexTotal/runCount);
}
}
我剛剛發現TryParse()
有一個問題,它佔數千分隔符。 En-US中的例子,10,36.00就可以。我有一個特定的情況,千位分隔符不應該被考慮,因此正則表達式\d(\.\d)
原來是最好的選擇。當然必須保留不同地區的十進制字符變量。
正如我對此感到羞恥,3.5中的TryParse確實有NumberStyles:下面的代碼也應該做的伎倆沒有正則表達式忽略數千分隔符。
double.TryParse(length, NumberStyles.AllowDecimalPoint,CultureInfo.CurrentUICulture, out lengthD))
與原始問題無關,但確認TryParse()確實是一個不錯的選擇。
一般情況下,即無限小數位:
^-?(([1-9]\d*)|0)(.0*[1-9](0*[1-9])*)?$
在.NET中,我建議動態構建與當前文化背景的小數點分隔符正則表達式:
using System.Globalization;
...
NumberFormatInfo nfi = NumberFormatInfo.CurrentInfo;
Regex re = new Regex("^(?\\d+("
+ Regex.Escape(nfi.CurrencyDecimalSeparator)
+ "\\d{1,2}))$");
您可能希望通過允許1000er分隔符與小數分隔符相同的方式來拉動正則表達式。
- 1. 正則表達式十六進制數
- 2. 十進制值的正則表達式
- 3. 正則表達式 - 十進制的
- 4. 正則表達式十進制到int
- 5. 正則表達式幫助十進制
- 6. 十進制正則表達式問題
- 7. Java十六進制正則表達式
- 8. 正則表達式 - 十進制
- 9. 有限制的十進制數的正則表達式
- 10. 十進制值的正則表達式限制16位數
- 11. 正則表達式表示jquery的非十進制數值
- 12. 非零負數十進制數的正則表達式
- 13. 正則表達式幫助:十進制正數,2位
- 14. 0或5的單位十進制數的正則表達式
- 15. 帶點和空白的十進制數的正則表達式
- 16. 正則表達式:用十進制數字和數字匹配
- 17. 正則表達式用逗號分隔的十進制數
- 18. 驗證十進制數的正則表達式
- 19. 有效2位十進制數的正則表達式
- 20. Yii中驗證十進制數的正則表達式
- 21. 從正則表達式字符串中的十進制數
- 22. 正則表達式爲字符串中的十進制數字
- 23. 獲取十進制數的正則表達式
- 24. 如何編寫捕獲十進制數的正則表達式?
- 25. 正則表達式序列的十進制數
- 26. c#正則表達式的數字範圍十進制
- 27. 彙編風格十六進制數的C#正則表達式
- 28. 正則表達式中的十進制格式
- 29. 正則表達式來檢測數字,但不是十進制
- 30. 規範化十進制數正則表達式
爲什麼你會在這裏使用正則表達式而不是框架解決方案(`decimal.TryParse`)? – 2009-11-22 18:42:14