1

I have the following string:

text = 'adsfklaiin2007daf adf adflkajf;j 2008afadfkjkj'

I want to return:

2007 2008

Any way to do this in Python?

coderman
  • 273
  • 2
  • 5
  • 10
  • check this answer: http://stackoverflow.com/questions/1450897/python-removing-characters-except-digits-from-string/1450900#1450900 – arie Apr 26 '11 at 15:30
  • What if it's a specific text I want to keep. It might be a number but it might not. For example, what if I wanted to keep 2007d 2008a or some other particular string of text? – coderman Apr 26 '11 at 15:45

4 Answers4

7

This is a classic case for regular expressions. Using the re python library you get:

re.findall('\d{4}', "yourStringHere")

This will return a list of all four digit items found in the string. Simply adjust your regex as needed.

Spencer Rathbun
  • 14,510
  • 6
  • 54
  • 73
5
import re
num = re.compile('[\d]*')
numbers = [number for number in num.findall(text) if number]
['2007', '2008']
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
3
>>> import re
>>> text = 'adsfklaiin2007daf adf adflkajf;j 2008afadfkjkj'
>>> re.sub("[^0-9]"," ",text)
'          2007                   2008         '

I will leave it to you to format the output.

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
1

str.translate

text.translate(None, ''.join(chr(n) for n in range(0xFF) if chr(n) not in ' 01234567890')

You can probably construct a better table of characters to skip and make it prettier, but that's the general idea.

nmichaels
  • 49,466
  • 12
  • 107
  • 135