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

构建针对特定文件掩码的两类文件名校验正则表达式

Regex Patterns for Validating Specific File Name Masks

I’ve put together the two regex patterns you requested, along with clear breakdowns so you can tweak them to fit your exact needs easily.

Pattern 1: 7-digit prefix + underscore + numeric suffix + specified extension

This pattern checks filenames that follow the rule:

  • Exactly 7 digits sit to the left of an underscore (_)
  • One or more digits sit to the right of the underscore
  • The file uses your specified extension(s)

Regex Code

^[0-9]{7}_[0-9]+\.(jpg|png|gif)$

Breakdown

  • ^: Anchors the match to the start of the filename (stops extra characters from appearing before the 7-digit prefix)
  • [0-9]{7}: Matches exactly 7 digits (0-9)
  • _: Matches the literal underscore character
  • [0-9]+: Matches one or more digits (use [0-9]{1,} if you prefer explicit syntax—+ is just shorthand)
  • \.: Escapes the dot (since dots have special meaning in regex, we need to tell it to match a literal dot)
  • (jpg|png|gif): Matches any of the listed extensions—swap these out for your actual required extensions (use just jpg if you only need one, e.g., \.jpg)
  • $: Anchors the match to the end of the filename (stops extra characters from appearing after the extension)

Example Matches

  • 1234567_1.jpg
  • 9876543_1234.png
  • 1122334_9.gif

Non-Matches

  • 123456_1.jpg (only 6 digits before the underscore)
  • 1234567_a.jpg (non-numeric character after the underscore)
  • 1234567_1.txt (unapproved extension)

Pattern 2: 10-digit prefix + space + 4-digit segment + underscore + numeric suffix + specified extension

This pattern validates filenames with this structure:

  • Exactly 10 digits sit to the left of a space
  • Exactly 4 digits sit to the right of the space
  • One or more digits sit to the right of an underscore
  • The file uses your specified extension(s)

Regex Code

^[0-9]{10} [0-9]{4}_[0-9]+\.(jpg|png|pdf)$

Breakdown

  • ^: Start of filename anchor
  • [0-9]{10}: Matches exactly 10 digits
  • : Matches a literal space (make sure this is a regular space, not a tab or other whitespace)
  • [0-9]{4}: Matches exactly 4 digits
  • _: Literal underscore character
  • [0-9]+: One or more digits after the underscore
  • \.: Escaped dot for the extension separator
  • (jpg|png|pdf): Replace with your required extensions (adjust the list as needed)
  • $: End of filename anchor

Example Matches

  • 1234567890 1234_5.jpg
  • 9988776655 4321_987.png
  • 1112223333 5555_1.pdf

Non-Matches

  • 123456789 1234_5.jpg (only 9 digits before the space)
  • 1234567890 123_5.jpg (only 3 digits after the space)
  • 1234567890 1234_a.jpg (non-numeric character after the underscore)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:15:42