Java正则表达式用于文件过滤 [英] Java regexp for file filtering

查看:41
本文介绍了Java正则表达式用于文件过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java构建一个正则表达式,该正则表达式将在FilenameFilter中传递以过滤目录中的文件.

I would like to build a regexp in Java that would be passed in a FilenameFilter to filter the files in a dir.

问题是我无法理解正则表达式思维模型"的窍门:)

The problem is that I can't get the hang of the regexp "mind model" :)

这是我想出的正则表达式,用于选择要排除的文件

This is the regexp that I came up with to select the files that I would like to exclude

(((ABC | XYZ))+ \ w * Test.xml

((ABC|XYZ))+\w*Test.xml

我想做的是选择所有以Test.xml结尾但不以ABC或XYZ开头的文件.

What I would like to do is to select all the files that end with Test.xml but do not start with ABC or XYZ.

您能否添加任何可以帮助我应对正则表达式的资源.

Could you please add any resources that could help me in my battle with regexps.

谢谢

以下资源说明了有关regexp的许多内容 regular-expressions.info

The following resource explains a lot of things about regexp regular-expressions.info

推荐答案

我想做的是选择所有以 Test.xml 结尾的文件但不要以 ABC XYZ 开头.

What I would like to do is to select all the files that end with Test.xml but do not start with ABC or XYZ.

您可以使用此正则表达式匹配所有文件:

Either you match all your files with this regex:

^(?:(?:...)(?<!ABC|XYZ).*?)?Test\.xml$

或者您做相反的事情,并获取每个不匹配匹配的文件:

or you do the opposite, and take every file that does not match:

^(?:ABC|XYZ).*?Test\.xml$

我个人认为第二种选择要简单得多.

Personally, I find the second alternative much simpler.


ABC_foo_Test.xml   // #2 matches
XYZ_foo_Test.xml   // #2 matches
ABCTest.xml        // #2 matches 
XYZTest.xml        // #2 matches
DEF_foo_Test.xml   // #1 matches
DEFTest.xml        // #1 matches
Test.xml           // #1 matches

这篇关于Java正则表达式用于文件过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆