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

正则表达式匹配需求求助:限制含1个点的10位数字字符

Solution for Your Regex Length Limitation Issue

Hey there! The problem with your current regex ^[0-9]{1,10}\.?[0-9]{0,10}$ is that it counts digits before and after the dot separately, allowing up to 10 + 1 + 10 = 21 characters total—way over your 10-character limit. Let's fix that with a regex that enforces the total length while respecting your other rules.

The Correct Regex

Here's a regex that meets all your requirements:

^(?=.{1,10}$)\d*\.?\d+$

Breakdown of How It Works

Let's break this down piece by piece to understand why it works:

  • ^ and $: Anchor the regex to match the entire input string (no partial matches).
  • (?=.{1,10}$): A positive lookahead that ensures the total length of the string is between 1 and 10 characters. This is the key to fixing your length issue.
  • \d*: Matches zero or more digits before the dot (if any).
  • \.?: Matches zero or one dot (since you allow at most one).
  • \d+: Matches one or more digits after the dot (if any) — this ensures the string can't be just a single dot (unless you want that, see the note below).

Valid Examples

This regex will match:

  • 1-10 digits (e.g., 1234567890, 5)
  • Strings with exactly one dot and total length 2-10 (e.g., 123.456, 123456789., .123456789, 5.)

If You Want to Disallow Leading/Trailing Dots

If you only want dots between digits (no leading . or trailing .), use this variation instead:

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

This requires:

  • Either 1-10 digits, or
  • A dot surrounded by at least one digit on both sides, with total length 2-10.

Why This Fixes Your Original Problem

By using the lookahead (?=.{1,10}$), we enforce the total character count first, before checking the content. This prevents the "10 before + 10 after" issue your original regex had.

内容的提问来源于stack exchange,提问作者Hoang Duy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 22:24:13