java获取isbn_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 !!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值