简体   繁体   English

正则表达式用于排除一些与起始字符串匹配的子文件夹

[英]regex for exclude some sub folders that match with a starting string

I am trying to set a regex pattern to allow parent folder with any characters(case insensitive) including special characters and exclude sub folder name whenever that match specific starting word with case insensitive. 我试图设置一个正则表达式模式,以允许父文件夹包含任何字符(不区分大小写)(包括特殊字符),并在子文件夹名称与不区分大小写的特定起始单词匹配时排除子文件夹名称。

I got temporary solution that excludes the sub folders, but in that regex i have to hard code all the sub folders that i want to exclude: 我得到了排除子文件夹的临时解决方案,但是在该正则表达式中,我必须对要排除的所有子文件夹进行硬编码:

[a-zA-Z0-9\\-]\\/(?!(st1|str2|str3))

Here users able to create any name as parent folder including special characters: 在这里,用户可以创建任何名称作为父文件夹,包括特殊字符:

its_MY-folder/foo
folder/bar

but need to restrict them whenever try to create a sub folder start with word "test" by ignoring case: 但在尝试创建以单词“ test”开头的子文件夹时,需要忽略大小写来限制它们:

MY_Folder-name/test_name
MY_Folder/Test-name

Thanks in advance for your help. 在此先感谢您的帮助。

The negative lookahead is only a placeholder, so you need to tell the regex parser what you want to match when there isn't the word test : 否定的前瞻仅是一个占位符,因此当没有单词test时,您需要告诉正则表达式解析器要匹配的内容:

^.*\/(?!(test|Test))[^\/]+$

Here is that regex tested online 这是正则表达式在线测试

You can see that after the negative lookahead (the (?!(test|Test)) bit) I say I want any character that isn't a / and the end of line $ , this way I ensure I'm at the end of the path. 您可以看到,在负向向前( (?!(test|Test))位)之后,我说我想要任何不是/字符以及$的结尾,这样我就可以确保我在结尾的路径。

In my regex I match the whole string, from start to end, but you can remove the ^.* at the beginning. 在我的正则表达式中,我从头到尾匹配整个字符串,但是您可以在开头删除^.*

The case insensitivity is usually a feature of the library you are using, so have a look on how to do it. 不区分大小写通常是您正在使用的库的功能,因此请看如何做。 For instance, if you use Linux and grep, you can do grep -i to be case insensitive. 例如,如果您使用Linux和grep,则可以执行grep -i使其不区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM