Мне нужен шаблон, который будет искать правильные ISBN в файле.
Только сам шаблон нужно реализовать с помощью регулярных выражений.
Мой код пока:
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";
Ввод:
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
Выход:
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
Ожидается, что он будет отозван
ISBN 978-5-317-04434-3
ISBN 978-5-93286-181-3
ISBN 5-272-00341-1
ISBN 5-272-00251-1
Подробнее здесь: https://stackoverflow.com/questions/784 ... with-isbns