You can see from regex101 that the regex ^\d{6}(\d{10})$
will check if there is 16digit from start to end of the String
(so check the length) and capture the last 10.
^
: start of the String
\d{6}
: first 6 digit
(\d{10})
: capture 10 digit
$
: end of the String
So you can use it in Java with
String s = "1234567890123456";
Pattern p = Pattern.compile("^\\d{6}(\\d{10})$");
Matcher m = p.matcher(s);
if(m.matches()){
System.out.println(m.group(1));
}
This can be checked on this ideone
You just have to add an else
clause to print that the String
is not valid.