Regex for ISBN-10 or ISBN-13 : ^(?:ISBN(?:-1[03])?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})
[- 0-9X]{13}$|97[89][0-9]{10}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)
(?:97[89][- ]?)?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$
public class RegexISNBDemo { public static void main(String[] args) { List<String> isbns = new ArrayList<String>(); //Valid ISBNs isbns.add("0-596-52068-9"); isbns.add("0 512 52068 9"); isbns.add("ISBN-10 0-596-52068-9"); isbns.add("ISBN-10: 0-596-52068-9"); //Invalid ISBNs isbns.add("0-5961-52068-9"); isbns.add("11 5122 52068 9"); isbns.add("ISBN-13 0-596-52068-9"); isbns.add("ISBN-10- 0-596-52068-9"); isbns.forEach((isbn)->{ boolean valid = RegexISNB.isValidISBNFormat(isbn); System.out.println("is "+isbn+" a valid ISBN? "+valid); }); } }
Result
is 0-596-52068-9 a valid ISBN? true is 0 512 52068 9 a valid ISBN? true is ISBN-10 0-596-52068-9 a valid ISBN? true is ISBN-10: 0-596-52068-9 a valid ISBN? true is 0-5961-52068-9 a valid ISBN? false is 11 5122 52068 9 a valid ISBN? false is ISBN-13 0-596-52068-9 a valid ISBN? true is ISBN-10- 0-596-52068-9 a valid ISBN? false
StringBuilder line = new StringBuilder(); line.append("Hey man"); line.append("\n"); line.append("I like Java"); line.append("\n"); boolean valid = StringRegex.isValidLines(line.toString(), 2); System.out.println("does \n===\n" + line.toString() + "===\nhas 2 lines? " + valid);October 26, 2019
Count number of words regex ^\\W*(?:\\w+\\b\\W*){1,”+length+”}$
public static Boolean isValidLength(String str, int length) { String regex = "^\\W*(?:\\w+\\b\\W*){1,"+length+"}$"; return Pattern.compile(regex).matcher(str).matches(); }
public class StringRegexDemo { public static void main(String[] args) { List<String> inputs = new ArrayList<String>(); inputs.add("Folaulau"); inputs.add("JAVA is great"); inputs.add("I love java programming"); inputs.forEach((input) -> { int length = 10; boolean valid = StringRegex.isValidLength(input, length); System.out.println("is " + input + " " + length + " in length? " + valid); }); } }
Result
is Folaulau 10 in length? true is JAVA is great 10 in length? true is I love java programming 10 in length? true
Password validation is the need for almost all the applications today. There are various ways to do validate passwords from writing everything manually to use third party available APIs.
static String regex = "((?=.*[a-z])(?=.*d)(?=.*[@#$%])(?=.*[A-Z]).{6,16})"; /* * (?=.*[a-z]) : This matches the presence of at least one lowercase letter. * (?=.*d) : This matches the presence of at least one digit i.e. 0-9. * (?=.*[@#$%]) : This matches the presence of at least one special character. * ((?=.*[A-Z]) : This matches the presence of at least one capital letter. * {6,16} : This limits the length of password from minimum 6 letters to maximum 16 letters. */ public static Boolean isValidPasswordFormat(String password) { if(password==null || password.length()==0) { return false; } return Pattern.compile(regex).matcher(password).matches(); }
US Zipcode Regex ^[0-9]{5}(?:-[0-9]{4})?$
/* * ^ # Assert position at the beginning of the string. * [0-9]{5} # Match a digit, exactly five times. * (?: # Group but don't capture: * - # Match a literal "-". * [0-9]{4} # Match a digit, exactly four times. * ) # End the non-capturing group. * ? # Make the group optional. * $ # Assert position at the end of the string. */
static String US_ZIPCODE_REGEX = "^[0-9]{5}(?:-[0-9]{4})?$"; public static Boolean isValidUSZipcodeFormat(String zipcode) { if(zipcode==null || zipcode.length()==0) { return false; } return Pattern.compile(US_ZIPCODE_REGEX).matcher(zipcode).matches(); }
public class ZipcodeDemo { public static void main(String[] args) { List<String> zips = new ArrayList<String>(); //Valid ZIP codes zips.add("12345"); zips.add("12345-6789"); //Invalid ZIP codes zips.add("123456"); zips.add("1234"); zips.add("12345-678"); zips.add("12345-67890"); for (String zip : zips) { boolean valid = ZipcodeRegex.isValidUSZipcodeFormat(zip); System.out.println(zip+" is a valid US zipcode? "+valid); } } }
Result
12345 is a valid US zipcode? true 12345-6789 is a valid US zipcode? true 123456 is a valid US zipcode? false 1234 is a valid US zipcode? false 12345-678 is a valid US zipcode? false 12345-67890 is a valid US zipcode? false
UK Zipcode Regex ^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$
static String UK_ZIPCODE_REGEX = "^[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}$"; public static Boolean isValidUKZipcodeFormat(String zipcode) { if(zipcode==null || zipcode.length()==0) { return false; } return Pattern.compile(UK_ZIPCODE_REGEX).matcher(zipcode).matches(); }August 6, 2019