Assuming that the number of digits are fixed in password and verify codes (Generally they are same as default values), We can extract digits from the string and then find substring which has verify code. This assumption is for simplicity.
String numberOnly= str.replaceAll("[^0-9]", "");
String verifyCode = numberOnly.substring(6);
Here String verifyCode = numberOnly.substring(6);
is getting last 5 digits of the string which is your verification code. You can also write numberOnly.substring(6,10);
to avoid confusions.
But this is prone to errors like StringIndexOutOfBoundsException
, So whenever you want to get substring which is starting from index i
till the end of the string, always write numberOnly.substring(i)
.