Python regex matches string

I have a python string I'm trying to extract. I have an interesting question:

>>> 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 am trying to run the number after ASIN. I still do not see the obvious problem. This corresponds to the beginning of the line, but not in the middle.

+5
source share
1 answer

You need to use re.search and grouping , and note that re.match matches the pattern from the beginning of the line:

 >>> 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 a length of 1 or more that appears after SKU and space!

+13
source

Source: https://habr.com/ru/post/1214604/


All Articles