Versst
Versst

Reputation: 11

What regex pattern can I use to work with ISBNs?

I require a pattern that will search for the correct ISBNs in the file.
Only the pattern itself needs to be implemented using regular expressions

My code so far:

using System;
using System.IO;
using System.Text.RegularExpressions;

namespace Libary
{
    class Program
    {
        static void LibrarySearcher(string file_books)
        {
            if (!File.Exists(file_books))
            {
                Console.WriteLine("Not found.");
            }

            string books = File.ReadAllText(file_books);

            string pattern = @"\b(?:ISBN(?:-1[03])?:?\s*)?[-0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]\b";

            
            MatchCollection matches = Regex.Matches(books, pattern);

            
            Console.WriteLine("Founded ISBN-s:");
            foreach (Match match in matches)
            {
                Console.WriteLine(match);
            }
        }


        static void Main(string[] args)
        {
            try
            {
                LibrarySearcher("dataBASE.txt");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

string pattern = @"\b(?:ISBN(?:-1[03])?:?\s*)?[-0-9]{1,5}[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9]+[- ]?[0-9X]\b";

Input:

ISBN 978-5-317-04434-3
ISBN 978-5-93286-181-3
ISBN 5-272-00341-1
ISBN 5-272-00251-1

ISBN 5-272---00251--1

ISBN 0-12-345678-9

Output:

ISBN 978-5-317-04434-3

ISBN 978-5-93286-181-3

ISBN 5-272-00341-1

ISBN 5-272-00251-1

272---00251

ISBN 0-12-345678-9

It is expected that it will be withdrawn

ISBN 978-5-317-04434-3

ISBN 978-5-93286-181-3

ISBN 5-272-00341-1

ISBN 5-272-00251-1

Upvotes: 0

Views: 55

Answers (0)

Related Questions