Hi, i am trying to find an ISBN substring:
Regex regex = new 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$)))");
Match m = regex.Match("one two ISBN-13: 978-0-470-04673-9 three");
Console.WriteLine(m.Value);
I got the regex from regexlib.com and understand it should match either on a 10 or 13 digit ISBN. Unfortunatly match always returns false. I would also like it to be able to find the ISBN even if there are no spaces either side, i.e. "one twoISBN-13: 978-0-470-04673-9three" if that is possible. So far not working, will take another look tomorrow (is 3am now!) but in the meantime if anyone has any pointers would be muchh appreciated. Regards, Chris | | ChrisMorley | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string pattern = @"ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)$";
// Valid
Console.WriteLine(Regex.IsMatch(@"ISBN 0 93028 923 4", pattern));
// Valid
Console.WriteLine(Regex.IsMatch(@"ISBN 1-56389-668-0", pattern));
// Invalid
Console.WriteLine(Regex.IsMatch(@"ISBN 123 456-789X", pattern));
// Invalid
Console.WriteLine(Regex.IsMatch("ISBN-13: 978-0-470-04673-9", pattern));
}
}
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | Hi John,
Many thanks for the reply.In addition to validation i am trying to extract the substring from a bigger string. I changed the code slightly and now have:
Console.WriteLine(Regex.Match(@"congress ISBN 0 93028 923 4 published catalog", pattern).Value);
with the additional words "congress published catalog" stuffed into the string, i would like for only the ISBN to be returned.
Going to take another looknow, but pointers appreciated as im already scratching my head.
Many thanks,
Chris | | ChrisMorley | Hello Chris,
Please try this sample code. Does it meet your needs?
string pattern = @"ISBN\x20(?=.{13})\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)";
//True Console.WriteLine(Regex.IsMatch(@"ISBN 0 93028 923 4", pattern));
//True Console.WriteLine(Regex.IsMatch(@"ISBN 1-56389-668-0", pattern));
//False Console.WriteLine(Regex.IsMatch(@"ISBN 123 456-789X", pattern));
//False Console.WriteLine(Regex.IsMatch("ISBN-13: 978-0-470-04673-9", pattern));
// ISBN 0 93028 923 4 Match m = Regex.Match(@"congress ISBN 0 93028 923 4 published catalog", pattern); Console.WriteLine(m.Value);
Regards, Jialiang Ge
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. | | Jialiang Ge [MSFT] | Thanks Jialiang. John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | Hi John and Jialiang,
I tried the code:
string pattern = @"ISBN\x20(?=.{13}$)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)"; Match m = Regex.Match(@"congress ISBN 1-56389-668-0 published catalog", pattern); Console.WriteLine(m.Value);
But no joy, with the above m.Value = "".
I would like m.Value = "ISBN 1-56389-668-0", and then i would expand to get MatchAll to get an array of Matches incase there are more than one ISBN substrings in the parent string.
Appreciate the help so far, again this is not me being lazy i will get into the details tomorrow and try to Do It Myself however i know Regular expressions are a big topic and i am new to them so have a few stairs to climb :) Unless i am doing something silly of course with the above, from what i have read in MSDN and my understanding is that it should work.
Regards,
Chris | | ChrisMorley | That doesn't even look like a legitimate ISBN format. Download Expresso Rgex test which is a free Regex tool and try to understand what we did. The software is free, but you will have to register it. Print out also the the 30 minute tutorial it comes with as an aid.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com | | JohnGrove | Thanks John! Hello Chris There is a subtle difference between the regex in your code string pattern = @"ISBN\x20(?=.{13} $)\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)"; and the one in mine: string pattern = @"ISBN\x20(?=.{13})\d{1,5}([- ])\d{1,7}\1\d{1,6}\1(\d|X)"; Please test my regex and let me know whether it meets your need. Regards, Jialiang Ge
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us. | | Jialiang Ge [MSFT] |
|