user3021955
user3021955

Reputation: 21

method .matches String false, how to have matches true

I'm trying to check if a string ends with a point and 2 or 3 characters. The regex I use is:

[.][a-z0-9A-Z][a-z0-9A-Z][a-z0-9A-Z]$

String example: qsdgfdssdh.nfo

It should return true but it always returns false.

Can you help me?

Thanks

Upvotes: 1

Views: 43

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

The String#matches function applies the pattern to the entire string. So the following should work:

String input = "qsdgfdssdh.nfo";
if (input.matches(".*\\.[0-9A-Za-z]{3}")) {
    System.out.println("match");
}

If you're wondering what your current pattern would match with String#matches, it would match .nfo:

String input = ".nfo";
if (input.matches("\\.[0-9A-Za-z]{3}")) {
    System.out.println("match");
}

Demo

Upvotes: 1

Related Questions