You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术需求:开发满足指定数值规则的正则表达式

Regex for Numeric Values with Up to Two Decimal Places (No Range Restrictions)

Got it, let's build the regex you need. This pattern will match any positive numeric value (integers or decimals with up to two digits after the decimal point) without limiting you to the 1-99.99 range:

^\d+(\.\d{1,2})?$

How This Regex Works

Let me break down each component so you understand exactly how it functions:

  • ^ : Anchors the match to the start of the string (ensures we don't pick up partial numbers buried in longer text)
  • \d+ : Matches one or more digits for the integer part—this supports any length, so 0, 5, 123, 456789 all work perfectly
  • (\.\d{1,2})? : The optional decimal section:
    • \. : Matches the literal decimal point
    • \d{1,2} : Matches 1 or 2 digits after the decimal (so .5, .99 are allowed, but .123 gets rejected)
    • ? : Makes the entire decimal part optional, so plain integers are fully supported
  • $ : Anchors the match to the end of the string (prevents extra invalid characters from being included after the number)

Want to Support Negative Numbers?

If you need to match negative values too, just add an optional negative sign at the start of the pattern:

^-?\d+(\.\d{1,2})?$

Test Cases

Matched Values:

  • 0
  • 12
  • 99.99
  • 100
  • 123.4
  • 5678.90
  • -42 (only with the negative-enabled regex)
  • -78.12 (only with the negative-enabled regex)

Unmatched Values:

  • 123.456 (three decimal places, exceeds the limit)
  • .78 (no integer part before the decimal)
  • 99. (decimal point with no digits after)
  • abc123 (non-numeric characters included)
  • 12 34 (space breaks the numeric format)

内容的提问来源于stack exchange,提问作者Sahil Bansal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 08:57:41