How can I match a Java regex on numbers and slashes (image resolution in a file path)

java, regex

Solution

You need to use find method..

`matches` would try to match the string exactly.

`find` could match in between the string provided you don't use `^`,`$`

See pattern.matcher() vs pattern.matches() for more info

So,your code would be like

boolean isValid=Pattern.compile(yourRegex).matcher(input).find();

But if you want to extract:

String res="";
Matcher m=Pattern.compile(yourRegex).matcher(input);
if(m.find())res=m.group();

Problem

I'm just trying to create a regex to recognise image resolutions in a file path. An example input string could be something like "/path/to/file/2048x1556/file.type". And all i want to be able to match on is the "/2048x1556" bit. I should not that the numbers of the resolutions can change, but will always be either 3 or 4 characters in length. I've tried so far using: ``` Pattern.matches("/\\d+x\\d+", myFilePathString) ``` An what feels like about 100 variations on that... I'm new to regex's so I'm sure it's something simple that I'm overlooking but I just can't seem to figure it out. Thanks in advance, Matt.

Original source

Related problems