Java String objects have a method matchesthat can be checked against a regular expression:
myString.matches("^\\d{4}(-\\d{4}){3}$")
This particular expression checks four digits and then three times (hyphen and four digits), thus representing your desired format.
source
share