Whether or not regular expressions are well suited for this task is debatable. Most people would probably argue that it's not.
As I understand it however, you have no choice as the API you're using takes a regular expression as argument, so here goes...
Code
public class NumericRangeRegexGenerator {
private static String baseRange(String num, boolean up, boolean leading1) {
char c = num.charAt(0);
char low = up ? c : leading1 ? '1' : '0';
char high = up ? '9' : c;
if (num.length() == 1)
return charClass(low, high);
String re = c + "(" + baseRange(num.substring(1), up, false) + ")";
if (up) low++; else high--;
if (low <= high)
re += "|" + charClass(low, high) + nDigits(num.length() - 1);
return re;
}
private static String charClass(char b, char e) {
return String.format(b==e ? "%c" : e-b>1 ? "[%c-%c]" : "[%c%c]", b, e);
}
private static String nDigits(int n) {
return nDigits(n, n);
}
private static String nDigits(int n, int m) {
return "[0-9]" + String.format(n==m ? n==1 ? "":"{%d}":"{%d,%d}", n, m);
}
private static String eqLengths(String from, String to) {
char fc = from.charAt(0), tc = to.charAt(0);
if (from.length() == 1 && to.length() == 1)
return charClass(fc, tc);
if (fc == tc)
return fc + "("+rangeRegex(from.substring(1), to.substring(1))+")";
String re = fc + "(" + baseRange(from.substring(1), true, false) + ")|"
+ tc + "(" + baseRange(to.substring(1), false, false) + ")";
if (++fc <= --tc)
re += "|" + charClass(fc, tc) + nDigits(from.length() - 1);
return re;
}
private static String nonEqLengths(String from, String to) {
String re = baseRange(from,true,false) + "|" + baseRange(to,false,true);
if (to.length() - from.length() > 1)
re += "|[1-9]" + nDigits(from.length(), to.length() - 2);
return re;
}
public static String rangeRegex(int n, int m) {
return rangeRegex("" + n, "" + m);
}
public static String rangeRegex(String n, String m) {
return n.length() == m.length() ? eqLengths(n, m) : nonEqLengths(n, m);
}
}
Usage
// Generate expression for range 123 - 321
String regexp = NumericRangeRegexGenerator.rangeRegex(123, 321);
Explanation
A brief explanation of the code follows.
Ranges on the shape 0000
-abcd
and abcd
-9999
First we note that matching ranges such as 0000
-abcd
is fairly easy.
An expression covering for instance 000
-527
can be expressed as
[0-4]
followed by two arbitrary digits, or
5
followed by 00
-27
(which is resolved recursively!)
Ranges on the shape 1000
-abcd
and abcd
-9999
are just as easy.
Lower limit, upper limit of different lengths.
If the "from"-number is shorter than the "to"-number it is fairly straight forward.
Assume for instance that the from
-number has 3 digits and the to
-number has 7 digits. The expression can then be composed as follows:
from
-999
(as described above),
- Any
4
, 5
or 6
digit number: [1-9][0-9]{3-5}
, or
1000000
-to
(as described above)
Lower limit / upper limit of equal lengths.
This is the trickiest situation (still not that tricky though!)
The solution is, again, best described by an example. Consider the range 273
- 548
. The expression can be composed by the following parts:
2
followed by 73
-99
(latter part described above),
[34]
followed by any two digits, or
5
followed by 00
-48
(latter part described above)