?
Solved

Regular Expression for ISBN

Posted on 2008-10-31
10
Medium Priority
?
1,602 Views
Last Modified: 2010-04-21
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?

Regards
0
Comment
Question by:logicslab
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 39

Expert Comment

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

ISBN[ -\d]+X?
0
 

Author Comment

by:logicslab
ID: 22849620
Sorry ISBN[ -\d]+X? will not work. It will allow
"ISBN sds sdds 13 978-1-847193-57-5 ssdsd"
0
 
LVL 5

Expert Comment

by:bitter_chicken
ID: 22849682
* my solution:
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])$

* Short explanation:

1) match IBSN or IBSN 13 with/without hyphens/spaces, non-backreferencing
ISBN[\x20\x2D]{0,1}(?:13[\x20\x2D]{0,1}){0,1}

2) first backreference group, \1, check for IBSN 13 prefix (978 or 979)
(978[\x20\x2D]|979[\x20\x2D]|)

3) check length of remaining (max 9 digits + 1 digit or X, max 3 total punctuation (- or space) )
(?=[0-9\x20\x2D]{9,12}[0-9xX]$)

4) second backreference, \2, 'group identifier' , i.e. language
(\d{1,5}?)[\x20\x2D]{0,1}

5) third backreference, \3, publisher
(\d{1,7})[\x20\x2D]{0,1}

6) fourth backreference, \4, item number
(\d{1,6}?)[\x20\x2D]{0,1}

7) final backreference, \5, checksum number
([0-9xX])

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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Expert Comment

by:ahoffmann
ID: 22849896
> 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?
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22849981
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.
0
 
LVL 16

Expert Comment

by:sh0e
ID: 22850740
by Michael Ash:
ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$
0
 

Author Comment

by:logicslab
ID: 22874522
hi

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

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])$

it is too wide
0
 
LVL 5

Accepted Solution

by:
bitter_chicken earned 2000 total points
ID: 22883565
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

0
 

Author Comment

by:logicslab
ID: 22902391
thank you.

It is working now :)
0
 

Author Closing Comment

by:logicslab
ID: 31511987
:)
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question