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

PCRE正则表达式需求:匹配需排除的文件路径规则

正则表达式解决方案:仅保留e-comps目录下的PDF文件

需求概述

需编写PCRE正则表达式,用于匹配需要排除的文件路径,最终仅保留e-comps目录或其子目录下的.pdf文件,同时需满足:

  • 不匹配任何不含斜杠的项(单个文件名/无路径的目录名)
  • 需排除两类内容:
    • 路径中不含e-comps的所有带扩展名文件
    • 路径中包含e-comps的非.pdf文件

示例输入

/client
/client/100-300
/client/100-300/101 abc
/client/100-300/101 abc/active
/client/100-300/101 abc/active/asset.pdf
/client/100-300/101 abc/active/asset.doc
/client/100-300/101 abc/e-comps/asset.pdf
/client/100-300/101 abc/e-comps/asset.doc
/client/100-300/101 abc/e-comps/asset.jpg
/client/100-300/101 abc/e-comps/folder/asset.jpg
/client/100-300/101 abc/e-comps/folder/asset.pdf
client
100-300
101 abc
active
asset.pdf
asset.doc
asset.jpg

预期匹配(需排除的项)

/client/100-300/101 abc/active/asset.pdf
/client/100-300/101 abc/active/asset.doc
/client/100-300/101 abc/e-comps/asset.doc
/client/100-300/101 abc/e-comps/asset.jpg
/client/100-300/101 abc/e-comps/folder/asset.jpg

最终正则表达式

^/.+(?<!e-comps/.+)\..+$|^/.+e-comps/.+\.(?!pdf$)[^/]+$

正则解析

该正则包含两个匹配分支,用|分隔,分别对应两类需排除的内容:

分支1:匹配不在e-comps目录下的所有带扩展名文件

^/.+(?<!e-comps/.+)\..+$
  • ^/:限定路径以斜杠开头,排除无路径的单个文件/目录
  • .+:匹配任意长度的路径内容
  • (?<!e-comps/.+):反向否定预查,确保路径中不包含e-comps目录及其子路径
  • \..+$:匹配带扩展名的文件(以点开头,后续任意字符直到行末)

分支2:匹配e-comps目录下的非PDF文件

^/.+e-comps/.+\.(?!pdf$)[^/]+$
  • ^/.+e-comps/.+:匹配包含e-comps目录的路径
  • \.(?!pdf$):正向否定预查,确保扩展名不是pdf
  • [^/]+$:匹配扩展名部分,确保目标是文件而非目录(避免匹配带点的目录名)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 06:54:54