Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi, i need to extract the isbn 10/13 code from the text i tried 2-3 regular expression but none of them working could anyone help me to solve my problem. following is my implementation if possible tell me what is wrong with my code.


C#
string strRegex = @"ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$";
            RegexOptions myRegexOptions = RegexOptions.None;
            Regex myRegex = new Regex(strRegex, myRegexOptions);
            string strTargetString = @"Thank you for picking up WPF 4 Unleashed! Windows Presentation Foundation (WPF) is Microsoft’s premier technology for creating Windows graphical user interfaces, whether they consist of plain forms, document-centric windows, animated cartoons, videos, immersive 3D environments, or all of the above. WPF is a technology that makes it easier than ever to create a broad range of applications. " + "\n" + @"ISBN-13: 978-0-672-33119-0 " + "\n" + @"ISBN-10: 0-672-33119-5" + "\n" + @"It’s also the basis for Silverlight, which has extended WPF technology onto the Web and into devices such as Windows phones.";

            foreach (Match myMatch in myRegex.Matches(strTargetString))
            {
                if (myMatch.Success)
                {
                    Console.WriteLine(myMatch.Groups[1].Value);
                }
            }
Posted
Updated 3-Jun-13 7:26am
v2

1 solution

Have a look here: http://regexlib.com/Search.aspx?k=ISBN&AspxAutoDetectCookieSupport=1[^]. There you'll find a solution ;)

In my opinion this regex:
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$)))
could works.


Matches
ISBN-13: 978-1-4028-9462-6
Non-Matches
ISBN: 1284233-2-1-1

More about ISBN's: http://www.isbn.org/standards/home/isbn/international/html/usm4.htm[^]


This works for sure:
ISBN(-1(?:(0)|3))?:?\x20(\s)*[0-9]+[- ][0-9]+[- ][0-9]+[- ][0-9]*[- ]*[xX0-9]

Tested on: Rad Software Regex Designer[^]
 
Share this answer
 
v3
Comments
mryeti 3-Jun-13 13:39pm    
i already tried them but they are not working :(
Maciej Los 3-Jun-13 13:40pm    
See my answer after update ;)
mryeti 3-Jun-13 13:58pm    
thnx mate its working now.
Maciej Los 3-Jun-13 14:13pm    
You're welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900