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

正则表达式捕获组匹配逻辑疑问:结果为何为FILE_NAME.EXTENSION

Understanding the Capture Group ([^.]+\.[^.]+) in Your Regex

Great question—let's break down this capture group step by step using your example string to make the logic crystal clear.

First, let's define each piece of the capture group:

  • [^.]: This is a negated character class. Inside square brackets, the ^ flips the matching rule: instead of matching specific characters, it matches any single character except a dot (.).
  • +: This quantifier means "match the preceding element one or more times." So [^.]+ translates to "grab one or more characters that are definitely not dots."
  • \.: An escaped dot, which matches a literal dot character (since an unescaped . in regex matches almost any character except a newline).

Applying this to your example string

After the ^\d+_ part matches the leading numbers and underscore, your remaining string is: FILE_NAME.EXTENSION.OTHEREXTENSION

Here's how the capture group processes this:

  1. The first [^.]+ starts at the F and matches every character until it hits the first dot. So it grabs FILE_NAME—it stops right at the . because [^.] can't match dots.
  2. The \. then matches that first literal dot between FILE_NAME and EXTENSION.
  3. The second [^.]+ starts at E and matches every character until it hits the next dot. This grabs EXTENSION—it stops at the . before OTHEREXTENSION for the same reason: [^.] won't match a dot, so it can't go past that point.

Putting it all together, the capture group ([^.]+\.[^.]+) lands exactly on FILE_NAME.EXTENSION—it can't include the .OTHEREXTENSION part because the second [^.]+ hits a dot and stops immediately.

A quick note on greediness

You might think greediness would make the regex match more, but that's not the case here. The [^.] rule is a hard exclusion: those segments cannot include dots, so the regex can't skip over dots to grab more characters. It's a strict "stop at the first dot" rule for each [^.]+ segment.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:03:51