JAVA|JAVA ISBN10 ISBN13 正则表达式

Regex for ISBN-10 : ^(?:ISBN(?:-10)?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})
[- 0-9X]{13}$)[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$
Regex for ISBN-13 : ^(?:ISBN(?:-13)?:? )?(?=[0-9]{13}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)
97[89][- ]?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9]$
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]$
Note: You cannot validate an ISBN using a regex alone, because the last digit is computed using a checksum algorithm. The regular expressions in this section validate the format of an ISBN only.
Now let’s test our ISBN regex using some demo ISBN numbers.
Validate ISBN-10 Formats Only
List isbns = new ArrayList(); //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"); String regex = "^(?:ISBN(?:-10)?:? )?(?=[0-9X]{10}$|(?=(?:[0-9]+[- ]){3})[- 0-9X]{13}$)[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]$"; Pattern pattern = Pattern.compile(regex); for (String isbn : isbns) { Matcher matcher = pattern.matcher(isbn); System.out.println(matcher.matches()); } Output: true true true true false false false false

Validate ISBN-13 Formats Only
List isbns = new ArrayList(); //Valid ISBNs isbns.add("ISBN 978-0-596-52068-7"); isbns.add("ISBN-13: 978-0-596-52068-7"); isbns.add("978 0 596 52068 7"); isbns.add("9780596520687"); //Invalid ISBNs isbns.add("ISBN 11978-0-596-52068-7"); isbns.add("ISBN-12: 978-0-596-52068-7"); isbns.add("978 10 596 52068 7"); isbns.add("119780596520687"); String regex = "^(?:ISBN(?:-13)?:? )?(?=[0-9]{13}$|(?=(?:[0-9]+[- ]){4})[- 0-9]{17}$)97[89][- ]?[0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9]$"; Pattern pattern = Pattern.compile(regex); for (String isbn : isbns) { Matcher matcher = pattern.matcher(isbn); System.out.println(matcher.matches()); } Output: true true true true false false false false

Validate ISBN-10 AND ISBN-13 Formats Both
List isbns = new ArrayList(); //Valid ISBNs isbns.add("ISBN 978-0-596-52068-7"); isbns.add("ISBN-13: 978-0-596-52068-7"); isbns.add("978 0 596 52068 7"); isbns.add("9780596520687"); 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("ISBN 11978-0-596-52068-7"); isbns.add("ISBN-12: 978-0-596-52068-7"); isbns.add("978 10 596 52068 7"); isbns.add("119780596520687"); isbns.add("0-5961-52068-9"); isbns.add("11 5122 52068 9"); isbns.add("ISBN-11 0-596-52068-9"); isbns.add("ISBN-10- 0-596-52068-9"); String regex = "^(?: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]$"; Pattern pattern = Pattern.compile(regex); for (String isbn : isbns) { Matcher matcher = pattern.matcher(isbn); System.out.println(matcher.matches()); } Output: true true true true true true true true false false false false false false false false

I will advise to play with above simple regular expression to try more variation of ISBNs and let me know your findings.
Happy Learning !!
【JAVA|JAVA ISBN10 ISBN13 正则表达式】原文地址:https://howtodoinjava.com/regex/java-regex-validate-international-standard-book-number-isbns/
仅做记录

    推荐阅读