简体   繁体   English

如何使用或不使用java正则表达式操作符

[英]How to use Or and Not java regex operatoers

Requirement: 需求:

I have to find a application called uninstaller under a directory, that directory contains few files like Uninstaller (incase of linux os ) Uninstaller.exe (in case of windows), Uninstaller.jar and Uninstaller.lax 我必须在一个目录下找到一个名为uninstaller的应用程序,该目录包含一些文件,如Uninstaller (包含linux操作系统) Uninstaller.exe (如果是windows), Uninstaller.jarUninstaller.lax

I tried 我试过了

final String pattern = "Uninstaller.*.(exe|[^lax]|[^jar])";
final FileFilter filter = new RegexFileFilter(pattern);
files = installDir.listFiles(filter);

but its returning Uninstaller.lax in case of linux! 但它在Linux的情况下返回Uninstaller.lax!

please help me overcome the isssue. 请帮我克服这个问题。

If I understand correctly, you want to match all Unistaller files that end with bin , jar , or lax . 如果我理解正确,您希望匹配以binjarlax结尾的所有Unistaller文件。 In that case, use this: 在这种情况下,使用此:

"Uninstaller\.(bin|jar|lax)"

Edit : 编辑

Ah, in that case, you can just match: 啊,在这种情况下,你可以匹配:

"^Uninstaller(\.exe)?$"

When you do this: 当你这样做:

[^lax]

You define a character class - a token that matches ANY character as long as it is not l, a, x. 您可以定义一个字符类 - 只要它不是l,a,x就匹配任何字符的标记。

So this token 所以这个令牌

(exe|[^lax]|[^jar])

means 手段

'match exe OR match any character that is not l/a/x OR match any character that is not j/a/r' '匹配exe或匹配任何不是l / a / x的字符或匹配任何不是j / a / r的字符

Clearly this is not what you want :) 显然这不是你想要的:)

You only want to match Uninstaller and Uninstaller.exe . 您只想匹配UninstallerUninstaller.exe So try 所以试试吧

Uninstaller(\\.exe)?$

(the $ means that the string must end) ($表示字符串必须结束)

In this case you don't have to explicitly say all the things you don't want to match, just the things you do. 在这种情况下,您不必明确说出您不想匹配的所有内容,只需要您所做的事情。

如果你想要的只是UninstallerUninstaller.exe ,那么你可以使用:

final String pattern = "Uninstaller(\.exe|$)";

尝试正则表达式

final String pattern = "Uninstaller(\\.exe)?";

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

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