17

I have a python string, that I'm trying to extract. I have a interesting issue:

>>> s="SKU 9780136058281, (ASIN B00A2KNZ2S, (binding Merchant: 'paperback' / 'hardcover'))"
>>> print(re.match('ASIN', s))
None
>>> print(re.match('SKU', s))
<_sre.SRE_Match object; span=(0, 3), match='SKU'>

I'm trying to mach a the number after ASIN. I can't still see a obvious problem. Its matching the beginning of the line, but not in the middle.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
Arun
  • 1,599
  • 5
  • 19
  • 33

1 Answers1

35

re.match tries to match the pattern from beginning of the string/text/etc. Instead, you need to use re.search and grouping:

>>> s="SKU 9780136058281, (ASIN B00A2KNZ2S, (binding Merchant: 'paperback' / 'hardcover'))"
>>> import re
>>> re.search(r'SKU (\d+)',s).group(1)
'9780136058281'

r'SKU (\d+) will match any combination of digits (\d) with length 1 or more that came after SKU and a space!

Mazdak
  • 105,000
  • 18
  • 159
  • 188