Python (re)

ISBN-10 in PY

Match 10-digit ISBNs, allowing optional hyphens or spaces between groups.

Try it in the PY tester →

Pattern

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

Python (re) code

pyPython
import re

pattern = re.compile(r"\b(?:\d[\- ]?){9}[\dXx]\b")
input_text = "0-306-40615-2"
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

Nine digits each followed by an optional hyphen or space, then a final check character which can be a digit or X (upper or lower case).

Examples

Input

0-306-40615-2

Matches

  • 0-306-40615-2

Input

0306406152

Matches

  • 0306406152

Input

080442957X

Matches

  • 080442957X

Same pattern, other engines

← Back to ISBN-10 overview (all engines)