Use Java direct concatenation using the plus sign.
if (mobile.matches("[0-9]{" + minValue + "," + maxValue + "}")) {
Indeed, as Michael suggested, compiling is better for performance if you use it a lot.
Pattern pattern = Pattern.compile("[0-9]{" + minValue + "," + maxValue + "}");
Then use it if necessary:
Matcher m = pattern.matcher(mobile); if (m.matches()) {
Martijn courteaux
source share