In case of a project of two friends of mine, i have been asked to write a regular expression pattern that matches ISBN 10 and ISBN 13 in different notation ways. So the result looks crappy but it works nearly perfect … here it is:
((?=(?:978|979)[0-9 ]{14})(?:\d{3}[ ]{1}\d{1,5}[ ]{1}\d{2,7}[ ]{1}\d{2,7}[ ]{1}\d{1})|(?=(?:978|979)[0-9-]{14})(?:\d{3}[-]{1}\d{1,5}[-]{1}\d{2,7}[-]{1}\d{2,7}[-]{1}\d{1})|(?=(?:978|979)\d{10})(?:\d{13})|(?=[0-9xX]{10})(?:(?:\d{9}[xX]{1}|\d{10}))|(?=[0-9-xX]{13})(?:\d{1}[-]{1}\d{3,5}[-]{1}\d{3,5}[-]{1}[0-9xX]{1})|(?=[0-9xX ]{13})(?:\d{1}[ ]{1}\d{3,5}[ ]{1}\d{3,5}[ ]{1}[0-9xX]{1}))
It matches:
- 1. 1234567891
- 2. 1234567891011
- 3. 3-446-19313-1
- 4. 3 446 19313 1
- 5. 978-3-86680-192-9
- 6. 978 3 86680 192 9
Notice: I’m far-off from being a regex guru so please don’t argue about the syntax