1

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?

1
  • I don't see why this pattern would work. What tool did you use? Commented Apr 8, 2015 at 18:00

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.