import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\[\\d{3,5} ?x ?\\d{3,5}\\])";
final String string = "Should match:\n\n"
+ "Hey guys check out this awesome hi-def picture! [320x480]\n\n"
+ "[F]irst time here plz be nice [6969x6969]\n\n"
+ "Spaces around the x [123 x 456]\n\n"
+ "herp derp durr (smallest dimensions that will match) [100x100]\n\n"
+ "Check out this massive 2.1 jiggapixel image of the entire USA in high detail! (highest dimensions that will match) [99999x99999]\n\n"
+ "Should not match:\n\n"
+ "Please go to this spammy site and buy shit so we can steal your credit card (no tag)\n\n"
+ "Letters in the tag [i234x5678]\n\n"
+ "Dimension too small [99 x 123]\n\n"
+ "Dimension too big [100000 x 123]\n\n";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html