2013-04-25 58 views
1

我真的无法找到正则表达式以十进制形式输入价格的任何解决方法。 这是我想要的: -以十进制表示验证价格的正则表达式

12345.1

12345.12

12345.123

0.123

0.123

我也想限制数字。

我真的创建了一个但假设没有验证

^([0-9] {1,5} |([0-9] {1,5} \([0-9] {1,3})))$

也想知道如何是上面的表达式从不同的一个

^([0-9] {1,5} |([0-9] 。([0-9] {1,3})))$这工作正常。

任何人都有很好的解释。

“我使用NSRegularExpression - 目标C” 如果这能帮助回答更准确

- (IBAction)btnTapped { 

     NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern: 
     @"^\\d{1,5}([.]\\d{1,3})?|[.]\\d{1,3}$" options:NSRegularExpressionCaseInsensitive error:&error]; 

    if ([regex numberOfMatchesInString:txtInput.text options:0 range:NSMakeRange(0, [txtInput.text length])]) 
     NSLog(@"Matched : %@",txtInput.text); 
    else 
     NSLog(@"Not Matched : %@",txtInput.text); 
} 

“我在buttonTap方法做这件事”。

+1

如果答案符合您的需求,而不是像您所做的那样将其置于原始问题中,则应该更好地[接受它](http://meta.stackexchange.com/a/5235)。这样,你的问题将被标记为*已解决*,然后可能对其他人有用。您已经提出的其他问题也一样。 – sp00m 2013-04-25 11:08:47

+0

我真的误添了你的表情,,,,,道歉相同 – 2013-04-25 11:27:18

回答

8

这简单的一应满足您的需求:

\d*[.]?\d+ 

“数字(\d+),前面可以有一个点([.]?),它本身可以前面有数字(\d*)。”

由于您在谈论价格,因此既不需要科学记数法也不需要负数。


正如兴趣点,这里是一个我常用,科学记数和负数包括:

[-+]?\d*[.]?\d+(?:[eE][-+]?\d+)? 

对于新的要求(见注释),你可以不指定我给第一个正则表达式需要多少位数,因为它不是内置的。由点本身其次是最大

\d{1,5}([.]\d{1,3})?|[.]\d{1,3} 

“最多5位(\d{1,5})可能随后((...)?)3位([.]\d{1,3}),或(|)简单地说:

这应该更好地满足您的需求一个点后跟最多3位数字([.]\d{1,3})“

+0

Ahahaha,这忽略了+/-和第一个数字之间的(可选的)空白的可能性... – 2013-04-25 10:32:58

+0

哦,等等,我错了,double.TryParse也不会在中间和数字之间留下空格。提交错误785552的.NET框架:) – 2013-04-25 10:41:16

+0

尝试123,123.45不工作使用NSRegularExpression - 目标C ,,,尚未解决... – 2013-04-25 11:06:57

0

如何:^([+-])?(\d+)?([.,])?(\d+)?$

  string input = "bla"; 
      if (!string.IsNullOrWhiteSpace(input)) 
      { 
       string pattern = @"^(\s+)?([-])?(\s+)?(\d+)?([,.])?(\d+)(\s+)?$"; 

       input = input.Replace("\'", ""); // Remove thousand's separator 
       System.Text.RegularExpressions.Regex.IsMatch(input, pattern); 
       // if server culture = de then reverse the below replace 
       input = input.Replace(',', '.'); 
      } 

编辑:
哦,哦 - 刚刚意识到这就是我们碰上如果EN-US用户使用问题的一点点“”作为千的分隔....

所以这里一个更好的:

 string input = "+123,456"; 
     if (!string.IsNullOrWhiteSpace(input)) 
     { 
      string pattern = @"^(\s+)?([+-])?(\s+)?(\d+)?([.,])?(\d+)(\s+)?$"; 

      input = input.Replace(',', '.'); // Ensure no en-us thousand's separator 
      input = input.Replace("\'", ""); // Remove thousand's separator 
      input = System.Text.RegularExpressions.Regex.Replace(input, @"\s", ""); // Remove whitespaces 

      bool foo = System.Text.RegularExpressions.Regex.IsMatch(input, pattern); 
      if (foo) 
      { 
       bool de = false; 
       if (de) // if server-culture = de 
        input = input.Replace('.', ','); 

       double d = 0; 
       bool bar = double.TryParse(input, out d); 
       System.Diagnostics.Debug.Assert(foo == bar); 

       Console.WriteLine(foo); 
       Console.WriteLine(input); 
      } 
      else 
       throw new ArgumentException("input"); 
     } 
     else 
      throw new NullReferenceException("input"); 

编辑2:
而不是通过获取服务器文化的麻烦,只是使用tryparse重载与文化,不要重新替代小数点分隔符。

double.TryParse(input 
       , System.Globalization.NumberStyles.Any 
       , new System.Globalization.CultureInfo("en-US") 
       , out d 
); 
+0

不需要放太多括号。而且,'(X +)?'应该减少到'X *'。完全是你的,但减少了:'^ \ s * [+ - ]?\ s * \ d * [。,]?\ d + \ s *?$'。另外,最后一个空格的*懒惰匹配并不是真的需要。 – sp00m 2013-04-25 10:54:34

1

让我们做这每个当事人之间:在开始

  • 登录:[+-]?
  • 分数号:\.\d+
  • 可能的组合(符号后):
    • 号码:\d+
    • 分数不为零\.\d+
    • 和数量与比例:\d+\.\d+

因此加入它一起<sign>(number|fraction without zero|number with fraction)

^[+-]?(\d+|\.\d+|\d+\.\d+)$ 
0

如果你不限制长度,小数点前5位和3位之后,那么你可以使用这个:最大前后

^[+-]?(?:[0-9]*\.[0-9]|[0-9]+)$ 

如果你是制约其5和3,那么你” ð需要的东西是这样的:

^[+-]?(?:[0-9]{0,5}\.[0-9]{1,3}|[0-9]{1,5})$ 

至于你的正则表达式的区别去,第一个限制的位数长度十进制标记之前1-5与不存在小数。第二个只允许在小数点前面有一个数字,如果没有小数点,则只允许1-5个数字。