十进制值的正则表达式

时间:2014-02-04 19:00:08

标签: .net regex c#-4.0

我想要一个正则表达式,它在文本框按键事件中采用以下十进制值

条件

  1. 最多只能输入7个整数部分。
  2. 最多可输入2位小数部分。
  3. 包含逗号分隔的12个字符,每千个&单个十进制字符(1,000,000.00),但应该能够从12个字符进行编辑。
  4. 可能的格式是,

    • 9999999
    • 9999998.55
    • 9999998.5
    • 0.55
    • 0.55
    • 1.5
    • 21.22
    • 2
    • 空 ''
    • 3
    • 0.00
    • 1,234567.10
    • 12,34567.00

    正则表达式,不包括逗号复杂度(仅满足条件1和2),我可以在代码中处理。我尝试了几个正则表达式,但找不到我需要的确切表达式。

    我尝试了以下代码,但它在我的keydown事件中无效。

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            bool isMatch = Regex.IsMatch(textBox1.Text,@"^(?!.*[\w-[\d]])  
                           (?=[\d.,]{0,12})  
                           (?=,{0,2})       
                           (?=\.{0,1})       
                           [\s\d]           
                           [\d,]{0,8}        
                           .?                
                           \d{0,2}           
                           \s?               
                            $");
           if (!isMatch)
           {
               e.Handled = e.Key != Key.Back;
           }
        }
    

4 个答案:

答案 0 :(得分:0)

我认为

^\d{1,7}\.?\d{0,2}$

应该用于检测正确的数字。

对于'空'值,您可以使用单独的测试。

答案 1 :(得分:0)

此模式不会阻止某人输入不完整的数字,例如(1,2),但会允许您请求的数字以及空格。 Null必须在传递之前被捕获。

 ^                # Work from the start of the line
 (?!.*[\w-[\d]])   # Don't allow any Alphanumerics (A-Za-z\d) but except digits \d
 (?=[\d.,]{0,12})  # Only allow for 0-12 digits or commas or periods
 (?!\d{8,})        # Stop match if eight consecutive digits found
 (?![\d,]{9,})     # Stop match if a combination of digits and commas exceeds 9
 [\s\d.]           # Allow for a space or a number period and consume it to the match.
 [\d,]{0,8}        # Should find numbers or a comma for up to 8 items
 .?                # A possible period
 \d{0,2}           # Two digits possible after the period
 \s?               # A space character (\r\n?) may be found or even an extra space (allowed per rules)
$

要使用此模式,必须使用IgnorePatternWhitespace选项,这意味着正则表达式解析器忽略模式中的空格和注释。它与加工无关。使用方法如下

Regex.IsMatch(text, pattern, RegexOptions.IgnorePatternWhitespace)

这是我的测试程序

    static void Main(string[] args)
    {
        Console.WriteLine("Negative Tests - Only False returned in this section"); 
        Console.WriteLine("12345678.00".IsMatch()); // False ->
        Console.WriteLine("123456789.00".IsMatch());
        Console.WriteLine("123a4567.00".IsMatch());
        Console.WriteLine("123.4567.00".IsMatch());
        Console.WriteLine ("1234567,8.00".IsMatch());
        Console.WriteLine ("1,234567,8.00".IsMatch());
        Console.WriteLine ("1,234567,.00".IsMatch());

        Console.WriteLine(Environment.NewLine);
        Console.WriteLine("Positive Tests - Only True returned in this section");
        Console.WriteLine("1234".IsMatch());  // True ->
        Console.WriteLine("9999998.55".IsMatch());
        Console.WriteLine("9999998.5".IsMatch());
        Console.WriteLine("0.55".IsMatch());
        Console.WriteLine(".55".IsMatch());
        Console.WriteLine("1.5".IsMatch());
        Console.WriteLine("21.22".IsMatch());
        Console.WriteLine("2.".IsMatch());
        Console.WriteLine("3".IsMatch());
        Console.WriteLine("0.00".IsMatch());
        Console.WriteLine("1,234567.10".IsMatch());
        Console.WriteLine("12,34567.00".IsMatch());
        Console.WriteLine(" ".IsMatch());

    }
}

public static class myRegexExtensions
{
    public static bool IsMatch(this string text)
    {
        var pattern = @"
  ^                # Work from the start of the line
 (?!.*[\w-[\d]])   # Don't allow any Alphanumerics (A-Za-z\d) but except digits \d
 (?=[\d.,]{0,12})  # Only allow for 0-12 digits or commas or periods
 (?!\d{8,})        # Stop match if eight consecutive digits found
 (?![\d,]{9,})     # Stop match if a combination of digits and commas exceeds 9
 [\s\d.]           # Allow for a space or a number period and consume it to the match.
 [\d,]{0,8}        # Should find numbers or a comma for up to 8 items
 .?                # A possible period
 \d{0,2}           # Two digits possible after the period
 \s?               # A space character (\r\n?) may be found or even an extra space (allowed per rules)
$    ";
        return Regex.IsMatch(text, pattern, RegexOptions.IgnorePatternWhitespace);
    }
}

答案 2 :(得分:0)

可能是,更好(更简单)的解决方案是按小数点分割字符串,然后分别测试左右部分。 让全能的正则表达本身并不是最终的结果。

答案 3 :(得分:0)

我不相信正则表达式就是这个的工具。如果规则“最多7个数字,其中散布着任意数量的逗号”,则无法用正则表达式表达它。如果您只想允许每3位数字的可选逗号的标准格式,则可能但主要涉及列出可能的组合:

(\d{0,3}|\d{1,3},?\d{0,3}|\d,?\d{1,3},?\d{0,3})

如果你不得不修改到不同的要求,这是非常难看和痛苦的。

用这个来验证格式要简单得多:

^([\d,]+)(\.\d{0,2})?$

然后通过计算Char.IsDigit为真的字符,对第一个匹配组进行额外验证。

相关问题