4

I need a regex to verify ISBN number entered by user.

ISBN must be a string contains only: [10 or 13 digits] and hyphens

I tried ^[\d*\-]{10}|[\d*\-]{13}$ but it doesn't work.

My regex only matches: 978-1-5661, 1-56619-90, 1257561035

It should returns the results below:

"978-1-56619-909-4 2" => false
"978-1-56619-909-4" => true
"1-56619-909-3 " => false
"1-56619-909-3" => true
"isbn446877428ydh" => false
"55 65465 4513574" => false
"1257561035" => true
"1248752418865" => true

I really appreciate any help.

manfcas
  • 1,933
  • 7
  • 28
  • 47
Louis Tran
  • 1,154
  • 1
  • 26
  • 46

2 Answers2

7

You can use this regex with a positive lookahead:

^(?=(?:\D*\d){10}(?:(?:\D*\d){3})?$)[\d-]+$

RegEx Demo

(?=(?:\D*\d){10}(?:(?:\D*\d){3})?$) is a positive lookahead that ensures we have 10 or 13 digits in the input.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 7
    People should be careful with this solution. It does match 10 or 13 digit numbers but not all 10 and 13 digit numbers are ISBNs. E.g. `978-0-306-40615-6` is 13 digits but is not a valid ISBN – TheGentleman Dec 21 '16 at 20:55
  • 1
    @anubhava: Thank you very much. Works perfectly. The answer will be marked Solved after 5 minutes. – Louis Tran Dec 21 '16 at 20:56
  • 1
    @GentlemanMax Yeah, you are right. This can't be used to verify a valid ISBN. But it is useful to check a valid ISBN format. – Louis Tran Dec 21 '16 at 20:59
  • 1
    @LouisTran, I agree. Just wanted to make sure it was mentioned her in case someone gets here via google down the line. – TheGentleman Dec 21 '16 at 21:00
  • 4
    Although it's outside the scope of the original question, as a note ISBN 10 can have the check digit be 'X' (standing for '10'). The above RegEx is very good, except it doesn't account for this possibility. I'm trying to leveral @anubhava's solution but include this exception (my RegEx powers are weak - that's why I'm here!). – Greg M Jul 27 '19 at 23:15
  • Can you please provide examples of those ISBN numbers? – anubhava Jul 28 '19 at 03:36
  • This is a valid ISBN not captured by your code @anubhava: 3-04-013341-X – samthebrand Oct 02 '20 at 18:11
  • @samthebrand My answer is based on this statement from OP `ISBN must be a string contains only: [10 or 13 digits] and hyphens` – anubhava Oct 02 '20 at 18:15
  • What about this question of mine? :( https://stackoverflow.com/questions/49895080/javascript-class-getter-setter – Sandrituky Jan 12 '22 at 05:36
5

As mentioned at the accepted answer, not all 10 or 13 digit numbers are valid ISBN.

An ISBN consists of five groups of numbers that make out 13 digits. In 2007 the standard moved from 10 digits. The five groups can accept various lengths of numbers, which makes ISBN challenging to validate. Ref. https://en.wikipedia.org/wiki/International_Standard_Book_Number

One solution is this: ^(?:ISBN(?:-13)?:?\ )?(?=[0-9]{13}$|(?=(?:[0-9]+[-\ ]){4})[-\ 0-9]{17}$)97[89][-\ ]?[0-9]{1,5}[-\ ]?[0-9]+[-\ ]?[0-9]+[-\ ]?[0-9]$ Source: O'Reilly Regular Expressions Cookbook, 2nd edition

You may find many possible regexp for ISBN validation here: https://regexlib.com/Search.aspx?k=ISBN

Thor Hovden
  • 413
  • 6
  • 13