简体   繁体   中英

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

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!

please help me overcome the isssue.

If I understand correctly, you want to match all Unistaller files that end with bin , jar , or lax . 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.

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'

Clearly this is not what you want :)

You only want to match Uninstaller and Uninstaller.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)?";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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