Reputation: 273
I have the following string:
text = 'adsfklaiin2007daf adf adflkajf;j 2008afadfkjkj'
I want to return:
2007 2008
Any way to do this in Python?
Upvotes: 1
Views: 1937
Reputation: 80851
import re
num = re.compile('[\d]*')
numbers = [number for number in num.findall(text) if number]
['2007', '2008']
Upvotes: 5
Reputation: 14910
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.
Upvotes: 7
Reputation: 343077
>>> 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.
Upvotes: 3
Reputation: 51029
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.
Upvotes: 1