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

正则表达式优化:存在点号时强制要求逗号的实现方法

Solution for Your Regex Requirement

Got it, let's fix that regex issue so it blocks unwanted formats like 1.000 while keeping all the valid input patterns you need.

Modified Regex

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

How It Works

This regex splits valid inputs into two clear scenarios, with a hard rule: any string containing a dot must include a comma:

  • Case 1 (No dot allowed): [0-9]+(?:,[0-9]{1,2})?
    • Matches pure integers (like 1)
    • Matches numbers with a decimal comma (like 123,56 or 1234,56)
  • Case 2 (Dot requires comma): [0-9]{1,3}\.[0-9]{1,3},[0-9]{1,2}
    • Enforces the exact valid dot-including pattern: 1-3 digits → dot → 1-3 digits → comma → 1-2 digits (perfect for your 1.234,56 example)
  • The ^ and $ anchors ensure we match the entire input string, so partial or malformed segments can't slip through.

Valid Matches (Allowed)

  • 1
  • 123,56
  • 1234,56
  • 1.234,56

Invalid Matches (Blocked as Expected)

  • 1.000 (has a dot but no comma)
  • 1.23 (has a dot but no comma)
  • 1, (incomplete decimal part)
  • .123,45 (missing leading digits before the dot)

内容的提问来源于stack exchange,提问作者Marco Vinicius Castellari

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:28:40