【问题标题】:Regex expression to match equal or greater than 1, by increments of .5正则表达式匹配等于或大于 1,增量为 0.5
【发布时间】:2015-11-11 00:39:35
【问题描述】:

经过多次尝试和研究,我找不到这个问题的正确解决方案:

需要匹配等于或大于1; 0.5 的增量也是有效的; 只有一位小数。

本质上我需要捕获 1 1.5 2 2.5 等等。

环顾四周,发现以下帮助: perl regex to find any number that is a multiple of 5

Regex greater than zero with 2 decimal places

How to match two strings with integers greater than zero using regex?

我试过了:

(?!\d[0.5])\d+|\.|\d*[05]\d{1}$

没有成功。

你能帮忙吗?

【问题讨论】:

  • 试试[1-9]\d*(\.5)?

标签: regex


【解决方案1】:

你可以使用这个正则表达式:

^[1-9]\d*(?:\.[05])?$

RegEx Demo

分手:

^        # Start of input
[1-9]    # match digit 1 to 9
\d*      # match 0 or more of any digits
(?:      # start of non-capturing group
   \.    # match a decimal
   [05]  # followed by digit 0 or 5
)?       # end non-capturing group. (? makes this group optional)
$        # End of input

【讨论】:

  • 我很高兴,我很接近
  • 哦,是的,除了一些小事之外,你完全是在赚钱。
猜你喜欢
  • 2014-11-08
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 2020-06-01
相关资源
最近更新 更多