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

请求生成验证5位数字前两位符合指定范围的正则表达式

Absolutely! This validation rule can be fully implemented with a regular expression. Let’s break this down clearly so you can understand every part, even if you’re new to regex.

First, let’s align your rules to regex logic

Your requirements translate to:

  1. The input must be 1 to 5 digits (since you specified "最多包含5位数字" — maximum 5 digits)
  2. If the input has 2 or more digits, the first two must fall into one of these ranges: 00-16, 20-25, 32-39

The Regular Expression (covers 1-5 digit inputs)

Here’s the regex that handles all valid cases:

^(?:(0[0-9]|1[0-6]|2[0-5]|3[2-9])\d{0,3}|\d)$

Let’s unpack each part

Let’s break down what every segment does so you know exactly what you’re using:

  • ^ : Ensures we start matching from the very first character of the input (no extra characters before the digits)
  • (?:...) : A non-capturing group (groups our valid two-digit options without storing the match for later use)
    • 0[0-9] : Matches any two-digit number from 00 to 09
    • 1[0-6] : Matches any two-digit number from 10 to 16
    • 2[0-5] : Matches any two-digit number from 20 to 25
    • 3[2-9] : Matches any two-digit number from 32 to 39
    • | : Acts like an "OR" — the regex will accept any of the preceding valid two-digit patterns
  • \d{0,3} : Matches 0 to 3 additional digits (combining with the two-digit prefix gives us valid lengths from 2 to 5 digits)
  • |\d : An extra "OR" to match single-digit inputs (since a single digit has no "first two digits" to validate)
  • $ : Ensures we match all the way to the end of the input (no extra characters after the digits)

If you only need to validate exact 5-digit inputs

Looking at your examples, all valid/invalid test cases are 5-digit numbers. If your actual requirement is exactly 5 digits (not up to 5), use this simplified regex instead:

^(0[0-9]|1[0-6]|2[0-5]|3[2-9])\d{3}$

This works because:

  • We match the valid first two digits, then exactly 3 more digits (\d{3}) to make a total of 5
  • The ^ and $ anchors guarantee there are no extra characters before or after the 5 digits

Testing with your samples

Let’s confirm against your provided examples:

  • Valid 5-digit inputs: 00789, 11569, 22698, 32567 → all match the regex
  • Invalid 5-digit inputs: 17895 (first two digits 17 are outside allowed ranges), 41578 (first two digits 41 are outside allowed ranges) → neither matches the regex

Quick code usage note

Since you’re using regex.IsMatch, you can drop this string directly into your regex library. For example (in C#):

string validationRegex = @"^(0[0-9]|1[0-6]|2[0-5]|3[2-9])\d{3}$"; // For exact 5-digit validation
bool isValid = Regex.IsMatch(yourInputNumber, validationRegex);

(Use the first regex string if you need to support 1-5 digit inputs.)

内容的提问来源于stack exchange,提问作者Nerdherder Ed

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:04:00