kgopi
kgopi

Reputation: 151

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.

Upvotes: 0

Views: 67

Answers (4)

Arun P Johny
Arun P Johny

Reputation: 388436

Try a regex like

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

Upvotes: 0

Aleks G
Aleks G

Reputation: 57346

If all you want is Uninstaller or Uninstaller.exe, then you can use:

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

Upvotes: 0

mart1n
mart1n

Reputation: 6233

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)?$"

Upvotes: 2

Patashu
Patashu

Reputation: 21793

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.

Upvotes: 0

Related Questions