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
  • 173,523
  • 149
  • 402
  • 512

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
  • 307,395
  • 66
  • 306
  • 293
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504