Regular Expression for ISBN

Hi Friends,

I having an ISBN regular expression ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$
but it only matches to these three formats ISBN 0 93028 923 4,ISBN 1-56389-668-0
and ISBN 1-56389-016-X.

I found some serious like this ISBN 1847193579 and ISBN 13 978-1-847193-57-5.

So can anybody suggest a better expression?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roger BaklundCommented:
Maybe you can use something simpler, like ISBN followed by any combination of digits, dashes and spaces, optionally ending with X?

ISBN[ -\d]+X?
logicslabAuthor Commented:
Sorry ISBN[ -\d]+X? will not work. It will allow
"ISBN sds sdds 13 978-1-847193-57-5 ssdsd"
* my solution:

* Short explanation:

1) match IBSN or IBSN 13 with/without hyphens/spaces, non-backreferencing

2) first backreference group, \1, check for IBSN 13 prefix (978 or 979)

3) check length of remaining (max 9 digits + 1 digit or X, max 3 total punctuation (- or space) )

4) second backreference, \2, 'group identifier' , i.e. language

5) third backreference, \3, publisher

6) fourth backreference, \4, item number

7) final backreference, \5, checksum number

8) end of line / string

** Sample replacement string

ISBN 978-\2-\3-\4-\5

-- This will output any given IBSN in the 'new' 13 digit format. Although, if the ISBN is given as a string without punctuation, it will just make a reasonable guess on where the hyphens should be placed
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

> but it only matches to these three formats ISBN 0 93028 923 4,ISBN 1-56389-668-0
hmm, 1-56389-668-0 seems not to be a valid ISBN number should be 1-56389-668-11

So is your goal just to match the correct format or to verify if it is valid?
Roger BaklundCommented:
ISBN[ -\d]+X? will not match "ISBN sds sdds 13 978-1-847193-57-5 ssdsd", but it will match "ISBN " in this case. You could add a requirement for a minimum number of digits.
by Michael Ash:
ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$
logicslabAuthor Commented:

i want use this regular expression with javascript. So how can i use this expression


it is too wide
I'm not sure what you mean about too wide. The attached code snippet works for me. Post your code if you have further problems.
var restring = /ISBN[\x20\x2D]{0,1}(?:13[\x20\x2D]{0,1}){0,1}(978[\x20\x2D]|979[\x20\x2D]|)(?=[0-9\x20\x2D]{9,12}[0-9xX]$)(\d{1,5}?)[\x20\x2D]{0,1}(\d{1,7})[\x20\x2D]{0,1}(\d{1,6}?)[\x20\x2D]{0,1}([0-9xX])$/;
var re = new RegExp(restring);
var subj = "ISBN 13 978-1-847193-57-5";
  if (subj.match(re)) {
    alert("Successful match");
  } else {
    alert("No match");

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
logicslabAuthor Commented:
thank you.

It is working now :)
logicslabAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.