Reputation: 2856
I have a list
:
['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
also as as string '14147618 (100%) 6137776 (43%) 5943229 (42%) 2066613 (14%) TOTAL\n'
Using regex, how do I return:
['14147618', '6137776, '5943229', 2066613']
Upvotes: 1
Views: 109
Reputation: 239473
You don't need RegEx at all, you can simply filter out the data which has only digits in them, with this list comprehension
print [item for item in data if item.isdigit()]
# ['14147618', '6137776', '5943229', '2066613']
Or you can also use filter
builtin function, like this
print filter(str.isdigit, data)
# ['14147618', '6137776', '5943229', '2066613']
Edit: If you have the entire data as a single string, you can split the data based on whitespace characters and then use the same logic
data = '14147618 (100%) 6137776 (43%) 5943229 (42%) 2066613 (14%) TOTAL\n'
print [item for item in data.split() if item.isdigit()]
# ['14147618', '6137776', '5943229', '2066613']
print filter(str.isdigit, data.split())
# ['14147618', '6137776', '5943229', '2066613']
Upvotes: 5
Reputation: 15501
Or if you want the even-indexed elements except for the last one:
print [data[i] for i in range(0,len(data)-1,2)]
Upvotes: 1
Reputation: 8400
No need to use re
module at all , you can use filter
over list
.
Try this ,
>>> a=['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
>>> filter(str.isdigit, a)
['14147618', '6137776', '5943229', '2066613']
>>>
Upvotes: 2
Reputation: 924
Use re module:
>>> import re
>>> [item for item in s if re.match(r'\d+',item)]
['14147618', '6137776', '5943229', '2066613']
Upvotes: 2
Reputation: 500367
Here is one way:
>>> l = ['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
>>> [el for el in l if re.match(r'\d+$', el)]
['14147618', '6137776', '5943229', '2066613']
Upvotes: 2
Reputation: 34146
As @thefourtheye said, it's not necessary to use regex at all, but if you really want to do it with regex, you can use:
import re
a = ['14147618', '(100%)', '6137776', '(43%)', '5943229', '(42%)', '2066613', '(14%)', 'TOTAL']
result = []
for e in a:
m = re.match(r'\d+', e)
if m is not None:
result.append(e)
print result
# ['14147618', '6137776', '5943229', '2066613']
Note: This can also be written as list comprehension:
print [e for e in a if re.match(r'\d+', e)]
Upvotes: 2