0

I have an alphanumeric code that looks like:

"PRODUCTNAME600COUPON50"

where PRODUCTNAME is a variable of inconsistent length

I want to be able to extract the integer values of the string into a list- in this case [600, 50].

I'm looking for a slick Pythonic way to do it- I started with this solution for finding the index of the first number within a string, but that falls short in this case.

Community
  • 1
  • 1
Yarin
  • 159,198
  • 144
  • 384
  • 498

1 Answers1

7

use the following regex:

In [67]: strs="PRODUCTNAME600COUPON50"

In [68]: re.findall(r'\d+',strs)
Out[68]: ['600', '50']

to get integers:

In [69]: map(int,re.findall(r'\d+',strs))
Out[69]: [600, 50]
SilentGhost
  • 287,765
  • 61
  • 300
  • 288
Ashwini Chaudhary
  • 232,417
  • 55
  • 437
  • 487