正则表达式捕获除以“.”开头的所有文件
在包含混合内容的目录中,例如:
.afile
.anotherfile
bfile.file
bnotherfile.file
.afolder/
.anotherfolder/
bfolder/
bnotherfolder/
如何捕获除以 .
开头的文件(而不是目录)之外的所有内容?
我尝试过使用负前瞻 ^(?!\.).+?
但它似乎无法正常工作。
请注意,我想通过使用 [a-zA-Z< 排除
.
来避免这样做。加上所有其他可能的字符减去点>]
有什么建议吗?
In a directory with mixed content such as:
.afile
.anotherfile
bfile.file
bnotherfile.file
.afolder/
.anotherfolder/
bfolder/
bnotherfolder/
How would you catch everything but the files (not dirs) starting with .
?
I have tried with a negative lookahead ^(?!\.).+?
but it doesn't seem to work right.
Please note that I would like to avoid doing it by excluding the .
by using [a-zA-Z< plus all other possible chars minus the dot >]
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该可以做到:
[^abc]
将匹配任何不是 a、b 或 cThis should do it:
[^abc]
will match anything that is not a, b or c转义
.
并否定可以开始您所拥有的名称的字符:使用您的测试用例成功测试
Escaping
.
and negating the characters that can start the name you have:Tested successfully with your test cases here.
负向先行
^(?!\.).+$
确实有效。这是 Java 版本:输出为(如 ideone.com 上所示):
另请注意非正则表达式
String.startsWith
。可以说这是最好、最易读的解决方案,因为无论如何都不需要正则表达式,并且startsWith
是O(1)
其中正则表达式(至少在 Java 中)是 <代码>O(N)。请注意空白字符串上的分歧。如果这是一个可能的输入,并且您希望它返回
false
,您可以编写如下内容:另请参阅
.*
即使在Pattern.DOTALL
模式下也需要O(N)
来匹配。The negative lookahead
^(?!\.).+$
does work. Here it is in Java:The output is (as seen on ideone.com):
Note also the use of the non-regex
String.startsWith
. Arguably this is the best, most readable solution, because regex is not needed anyway, andstartsWith
isO(1)
where as the regex (at least in Java) isO(N)
.Note the disagreement on the blank string. If this is a possible input, and you want this to return
false
, you can write something like this:See also
.*
even inPattern.DOTALL
mode takesO(N)
to match.嗯...负面角色类别怎么样?
排除点?
Uhm... how about a negative character class?
to exclude the dot?