tzortzik
tzortzik

Reputation: 5133

regex pattern in java is not working

I am trying to create a regex pattern to search for a special kind of file names:

A file name may look like this:

fileName_1x1.extension

I want to find if the filename has this pattern:

_(number)x(number).

I pasted the previous path into an online regex generator/tester and it worked with this pattern:

Pattern pattern = Pattern.compile("_\\d(.+)x\\d(.+)\\.");
Matcher matcher = pattern.matcher("fileName_1x1.extension");
return matcher.find();

Why isn't this working in Java?

Upvotes: 1

Views: 69

Answers (1)

anubhava
anubhava

Reputation: 785146

It should be this regex:

Pattern pattern = Pattern.compile("_\\d+x\\d+\\.");

You have .+ after \\d which will match any character 1 or more time after a digit.

Upvotes: 4

Related Questions