首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正则表达式匹配文件夹和所有子文件夹

正则表达式匹配文件夹和所有子文件夹
EN

Stack Overflow用户
提问于 2016-01-09 17:28:51
回答 2查看 35.9K关注 0票数 7

我需要为备份排除筛选器编写一个正则表达式,以排除一个文件夹及其所有子文件夹。

我需要匹配以下内容

folder1/statistics folder1/statistics/* folder2/statistics folder2/statistics/*

我想出了这个正则表达式,它匹配文件夹统计信息,但不匹配统计信息文件夹的子文件夹。

[^/]+/statistics/

如何扩展此表达式以匹配statistics文件夹下的所有子文件夹?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-09 17:33:31

使用以下正则表达式:

代码语言:javascript
运行
复制
/^[^\/]+\/statistics\/?(?:[^\/]+\/?)*$/gm

Demo on regex101

解释:

代码语言:javascript
运行
复制
/
  ^           # matches start of line
 [^\/]+       # matches any character other than / one or more times
 \/statistics # matches /statistics
 \/?          # optionally matches /
 (?:          # non-capturing group
   [^\/]+     # matches any character other than / one or more times
   \/?        # optionally matches /
 )*           # zero or more times
 $            # matches end of line
/
g             # global flag - matches all
m             # multi-line flag - ^ and $ matches start and end of lines
票数 15
EN

Stack Overflow用户

发布于 2020-12-09 19:38:28

如果使用|(或):

代码语言:javascript
运行
复制
'.*/statistics($|/.*)'

解释:

代码语言:javascript
运行
复制
.*             # any length string
/statistics    # statistics directory
($|/.*)        # end of string or any string starting with /

它做到了这一点,而且不难理解。使用python re模块进行了测试。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34691809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档