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})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.
[- 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]$
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/
仅做记录
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)
- java之static、static|java之static、static final、final的区别与应用
- Java基础-高级特性-枚举实现状态机