fork download
  1. import re
  2. texts = ['ISBN-13: 978 1 4310 0862 9',
  3. 'ISBN: 9781431008629',
  4. 'ISBN9781431008629',
  5. 'ISBN 9-78-1431-008-629',
  6. 'ISBN: 9781431008629 more text of the number',
  7. 'isbn : 9781431008629']
  8. rx = re.compile(r'ISBN(?:-13)?\D*(\d(?:\W*\d){12})', re.I)
  9. for text in texts:
  10. m = rx.search(text)
  11. if m:
  12. print(text, '=> ISBN:', ''.join([d for d in m.group(1) if d.isdigit()]))
  13.  
Success #stdin #stdout 0.03s 9588KB
stdin
Standard input is empty
stdout
ISBN-13: 978 1 4310 0862 9 => ISBN: 9781431008629
ISBN: 9781431008629 => ISBN: 9781431008629
ISBN9781431008629 => ISBN: 9781431008629
ISBN 9-78-1431-008-629 => ISBN: 9781431008629
ISBN: 9781431008629 more text of the number => ISBN: 9781431008629
isbn : 9781431008629 => ISBN: 9781431008629