My guess is that maybe you might want to design an expression to cover some other single and double digits, beside three digits, such as:
^([0-1][0-9]{2}|[0-9]{2}|[0-9])$
or,
^([0-1][0-9]{2}|[0-9]{1,2})$
or,
^(?:[0-1][0-9]{2}|[0-9]{1,2})$
Test
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class re{
public static void main(String[] args){
final String regex = "^(?:[0-1][0-9]{2}|[0-9]{2}|[0-9])$";
final String string = "0\n"
+ "1\n"
+ "2\n"
+ "00\n"
+ "11\n"
+ "22\n"
+ "99\n"
+ "100\n"
+ "000\n"
+ "199\n"
+ "200";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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));
}
}
}
}
Output
Full match: 0
Full match: 1
Full match: 2
Full match: 00
Full match: 11
Full match: 22
Full match: 99
Full match: 100
Full match: 000
Full match: 199
RegEx Circuit
jex.im visualizes regular expressions:
