5

Hi all I'm trying all the time to make a regular expression for XML in my .xsd.

This expression should verify an ISBN-13 number but I couldn't get further than this:

ISBN (978|979)[ |-][0-9]{1,5}[ |-][0-9]{1,7}[ |-][0-9]{1,7}[0-9]{1}

This is ok if somebody fills in a correct ISBN13, but it is also still possible to make ISBNs which are much longer (for example it is still possible to make the first block of numbers 5 long and the second 7 digits and the third also 7 digits long).

I am very new to this topic and I cannot solve this problem, therefore I hope somebody could help me or solve my problem.

I know what an ISBN 13 consists of, I checked wikipedia and other websites, but my actual problem is the regular expression it self.

I hope that this wasnt asked earlier, so I am sorry if somebody asked this question earlier. jajay07

Gumbo
  • 643,351
  • 109
  • 780
  • 844
user534202
  • 51
  • 1
  • 2

3 Answers3

8

According to http://regexlib.com/REDetails.aspx?regexp_id=1747 the regular expression

ISBN(-1(?:(0)|3))?:?\x20+(?(1)(?(2)(?:(?=.{13}$)\d{1,5}([ -])\d{1,7}\3\d{1,6}\3(?:\d|x)$)|(?:(?=.{17}$)97(?:8|9)([ -])\d{1,5}\4\d{1,7}\4\d{1,6}\4\d$))|(?(.{13}$)(?:\d{1,5}([ -])\d{1,7}\5\d{1,6}\5(?:\d|x)$)|(?:(?=.{17}$)97(?:8|9)([ -])\d{1,5}\6\d{1,7}\6\d{1,6}\6\d$)))

matches both the old 10 digit ISBNs and the new 13 digit ISBNs.

You can easily (maybe not very easily) use the part of this regexp that you need.

terminus
  • 13,745
  • 8
  • 34
  • 37
  • I found this solution also at the same page, unfortunatly it doesnt work with my xml and my xsd – user534202 Dec 07 '10 at 21:07
  • What your exact problem with this sollution? – terminus Dec 07 '10 at 21:08
  • maybe it is my very limited regEX skills, but i cannot understand how this one is build up or how i can get there from my approach, I also want to understand what this does and not only copy paste.. besides that I must not have ISBN10. The field may only contain ISBN13 – user534202 Dec 07 '10 at 21:15
  • If you don't want the 10-digit version, then dmnkhhn has a better answer. – terminus Dec 07 '10 at 21:16
4
ISBN(?:-13)?:?\x20*(?=.{17}$)97(?:8|9)([ -])\d{1,5}\1\d{1,7}\1\d{1,6}\1\d$

Should match:
ISBN-13: 978-1-4028-9462-6
ISBN: 978-1-4028-9462-6
ISBN-13 978-1-4028-9462-6
ISBN 978-1-4028-9462-6

dmnkhhn
  • 1,013
  • 1
  • 10
  • 18
  • i just coupied this one to try it, but the xsd doesnt validate after the ISBN occoures an error. maybe I'm to stupid but this drives all day, can anybody explain me what "happens" in this part \x20*(?=.{17}$)97(?:8|9) – user534202 Dec 07 '10 at 21:17
  • What if I needed the expression to match patterns that do not contain the heading "ISBN" part? – Nano Taboada Apr 18 '11 at 14:32
  • Just remove the `ISBN(?:-13)?:?\x20*` part – dmnkhhn Apr 20 '11 at 07:47
2

See also ISBN on xFront :

We have created an XML Schema simpleType definition for ISBNs. This ISBN definition covers all the legal formats of ISBNs world-wide.

Istao
  • 7,425
  • 6
  • 32
  • 39