0

I have this string

mod = 'ketobutyric_arp_rm(12);oxidation+%28hw%29(19)'

and want to get the numbers in parentheses as a list some kind of similar to:

mod_pos = ['12','19']

Using split seems to be a bit circuitous, and I don't know how to use a find method on this.

Any suggestions how I can do this?

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
  • 1
    possible duplicate of [Python and Regex - extracting a number from a string](http://stackoverflow.com/questions/7507524/python-and-regex-extracting-a-number-from-a-string) – matsjoyce Oct 28 '14 at 15:34
  • 1
    possible duplicate of http://stackoverflow.com/questions/4289331/python-extract-numbers-from-a-string – Mironor Oct 28 '14 at 15:38
  • 1
    Could just use a regex: http://rubular.com/r/rsyO4rZzuS – Tadgh Oct 28 '14 at 15:42

2 Answers2

1

Here is one way:

>>> import re
>>> mod='ketobutyric_arp_rm(12);oxidation+%28hw%29(19)'
>>> re.findall(r'\((\d+)\)', mod)
['12', '19']
Irshad Bhat
  • 8,479
  • 1
  • 26
  • 36
0

Use regular expressions. import re m=re.match(r'((\d+))', string) This would return matched patterns in a tuple which u can fetch by m.group(1) etc..