Python (re)

ISBN-13 in PY

Match 13-digit ISBNs starting with 978 or 979, with optional hyphens or spaces.

Try it in the PY tester →

Pattern

regexPY
\b97[89][\- ]?(?:\d[\- ]?){9}\d\b   (flags: g)

Python (re) code

pyPython
import re

pattern = re.compile(r"\b97[89][\- ]?(?:\d[\- ]?){9}\d\b")
input_text = "978-0-306-40615-7"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

The 978 or 979 GS1 prefix anchors the match, followed by nine digits (with optional separators) and a final check digit.

Examples

Input

978-0-306-40615-7

Matches

  • 978-0-306-40615-7

Input

9780306406157

Matches

  • 9780306406157

Same pattern, other engines

← Back to ISBN-13 overview (all engines)