简体   繁体   中英

Use regular expression to remove contents in brackets in python

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']

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']

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)]

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']

Use re module:

>>> import re
>>> [item for item in s if re.match(r'\d+',item)]
['14147618', '6137776', '5943229', '2066613']

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']
>>>

或者,如果要除最后一个元素外的偶数索引元素:

print [data[i] for i in range(0,len(data)-1,2)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM