isbn_find#

isbn_find(haystack, *, validate=True)#

Find the first ISBN in the given haystack.

This function searches for the first ISBN in the given haystack. By default, all ISBNs are validated (check digit verification); if invalid ISBNs are to be included as well, the validate flag must be set to False.

The underlying regular expression is based on the approach described in the book Regular Expressions Cookbook by Jan Goyvaerts and Steven Levithan [1]. It finds ISBNs in both the ISBN-10 and ISBN-13 formats. The individual components of an ISBN can be separated either by a space (SPACE U+0020) or a hyphen (HYPHEN-MINUS U+002D).

[1]

Jan Goyvaerts and Steven Levithan. O’Reilly, Beijing u.a., 2nd ed., ISBN 978-1-4493-1943-4, 2012, p. 292.

Added in version 0.3.0.

Warning

This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.

Parameters:
haystackstr

The string to be searched.

validatebool, default=True

If set, the value found is verified to ensure that it is a formally valid ISBN.

Returns:
Match | None

The first match, if any.

See also

isbn_findall

Find all non-overlapping ISBNs in the given haystack.

Examples

>>> from marc21_learn.pid import isbn_find
>>>
>>> isbn_find("ISBN 978-3-10-031815-2")
Match(value: '978-3-10-031815-2', start: 5, end: 22)
>>> isbn_find("ISBN-13: 9783100318152")
Match(value: '9783100318152', start: 9, end: 22)

The result can be converted to a string by using the __str__ interface:

>>> if (isbn := isbn_find("978-0-571-08989-5")):
...     print(isbn)
...
978-0-571-08989-5