正则表达式介于-10和10之间的值,最多1个小数位 [英] Regex for values between -10 and 10 up to 1 decimal place

查看:361
本文介绍了正则表达式介于-10和10之间的值,最多1个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ac#Web应用程序,该应用程序具有要添加正则表达式的控件,但是由于我对正则表达式没用,所以我希望有人能够为我提供帮助!

I have a c# web application that has a control in that I would like to add a regular expression to, but as I'm useless with regular expressions I was hoping someone would be able to help me!

我想要一个正则表达式,它将接受介于-10和10之间的所有值,并允许最多1个可选的小数位。

I would like a regex that will accept all values between -10 and 10 and allow up to 1 optional decimal place.

输入成功

1
-2.1
3.7
-4
5.8
10
-10

输入失败

10.1
-10.1
3.14159265359
-3.14159265359 and so on

我在我的应用程序中已经有一个类似的正则表达式 ^(0(\ .\d {1,4})?| 1(\.0 {1,4})?)$ 。这只接受4dp的正值和负值,我可以更改它(见下文),但是我不确定如何将-10到10的范围包括在内。

I already have a somewhat similar regex in my application ^(0(\.\d{1,4})?|1(\.0{1,4})?)$. This only accepts positive and negative values to 4dp, I've had a go at changing it (see below) but I'm unsure of how to include the -10 to 10 range.

^(0(\.\d{1,1})?|1(\.0{1,1})?)$

预先感谢

推荐答案

您可以使用

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

请参见 regex演示

如果您不想将 10 0匹配除去小数部分的(?: \.0)?

If you do not want to match 10 with 0 in the fractional part, remove (?:\.0)?.

详细信息


  • ^ -字符串的开头

  • -?-1或0 -符号

  • (?: 10(?:\.0) ?| [0-9](?: \。[0-9])?)-2个替代方法:


    • 10(?:\.0)?- 10 可选,后跟 .0

    • | -或

    • [0-9](?: \。[0-9])?-从 0 9 ,后跟可选的和任意一位数字

    • ^ - start of string
    • -? - 1 or 0 - symbols
    • (?:10(?:\.0)?|[0-9](?:\.[0-9])?) - 2 alternatives:
      • 10(?:\.0)? - 10 optionally followed with .0
      • | - or
      • [0-9](?:\.[0-9])? - any 1 digit from 0 to 9 followed with an optional sequence of . and any 1 digit

      这篇关于正则表达式介于-10和10之间的值,最多1个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆