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

正则表达式匹配咨询:匹配开头为2-3位数字且不含指定模式的行

分析你的正则表达式及修正建议

Hey there! Let's take a close look at your regex and verify if it's fully aligned with your requirements, plus fix a couple of potential gaps you might have missed.

First, let's recap your exact requirements

You want to match entries that:

  • Start with 2 or 3 digits (not 1 digit) (and it looks like you don't want leading zeros, given your test case)
  • Do NOT contain any of the strings: FO, SH, GDP, or LDP

How your current regex works

Your regex: ^[1-9][1-9]((?!FO|SH|GDP).)*$
Let's break it down piece by piece:

  • ^[1-9][1-9]: Ensures the entry starts with two non-zero digits
  • ((?!FO|SH|GDP).)*: A negative lookahead that guarantees no "FO", "SH", or "GDP" appear anywhere in the rest of the entry
  • $: Anchors the match to the end of the entry, so we only match full entries, not partial ones

Why it worked for your test case

Your target entry 153/G6S.3-H;2-3;1-2 gets matched because:

  • The first two digits 15 satisfy [1-9][1-9]
  • The remaining part 3/G6S.3-H;2-3;1-2 doesn't contain any excluded strings, so the rest of the regex matches it perfectly
  • All other entries either include excluded terms (like FO, SH, GDP) or start with a single digit (like 1/G13S.2-3), so they're correctly rejected

Potential gaps in your current regex

While it works for your test data, there are two issues that might cause problems with other entries:

  1. It doesn't limit the starting digits to 2 or 3
    Your regex allows entries starting with 4+ digits (e.g., 1234/XYZ with no excluded terms would be matched, but it starts with 4 digits, which violates your "2 or 3 digits only" rule)
  2. It doesn't exclude LDP
    Your requirement says to exclude entries containing LDP, but your regex only checks for FO, SH, and GDP. An entry like 12/ABC/LDP would be incorrectly matched.

The corrected regex

To fully meet your requirements, use this refined regex:

^[1-9][0-9]{1,2}(?:(?!FO|SH|GDP|LDP).)*$

Let's explain the key improvements:

  • ^[1-9][0-9]{1,2}:
    • [1-9]: Ensures the first digit isn't zero
    • [0-9]{1,2}: Matches 1 to 2 more digits, so total starting digits are exactly 2 or 3 (exactly what you need)
  • (?:(?!FO|SH|GDP|LDP).)*:
    • Added LDP to the excluded terms list to cover all your requirements
    • (?:...) is a non-capturing group (it doesn't store unnecessary match data, making the regex more efficient)
    • The negative lookahead ensures none of the excluded strings appear anywhere in the entry
  • $: Ensures we match the entire entry, not just a part of it

Testing the corrected regex against your entries

  • ✅ 153/G6S.3-H;2-3;1-2: Matches (3 starting digits, no excluded terms)
  • ❌ 12(3)/FO.2-3;1-2: Rejected (contains FO)
  • ❌ 1/G13S.2-3: Rejected (starts with 1 digit)
  • ❌ 22/FO.2-3;1-2: Rejected (contains FO)
  • ❌ 12(3)2S/FO.2-3;1-2: Rejected (contains FO)
  • ❌ 153/SH/G6S.3-H;2-3;1-2: Rejected (contains SH)
  • ❌ 45/3/H/GDP6;2-3;1-2: Rejected (contains GDP)

This regex will handle all edge cases aligned with your original requirements.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:23:14