import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("\\d{3,5}");
String string = "123-4561-789-2fv";
Matcher matcher = pattern.matcher(string);
boolean result = matcher.matches();
System.out.println(result);
matcher.reset();
System.out.println(matcher.find());
System.out.println(matcher.start()+"-"+matcher.end());
System.out.println(matcher.find());
System.out.println(matcher.start()+"-"+matcher.end());
System.out.println(matcher.find());
System.out.println(matcher.start()+"-"+matcher.end());
System.out.println(matcher.find());
//System.out.println(matcher.start()+"-"+matcher.end());
System.out.println(matcher.lookingAt());
System.out.println(matcher.lookingAt());
System.out.println(matcher.lookingAt());
System.out.println(matcher.lookingAt());
}
}
output:
false
true
0-3
true
4-8
true
9-12
false
true
true
true
true