使用正则表达式从字符串列表解析ID

时间:2018-08-29 17:09:41

标签: python regex

如果您有字符串列表:

Z100 = [
    '[ship, \'house\', "boat": 65, "price":10000, "car_id":"128bhsdfb03-2389-1379be8", "age":15]',
    '[ ]',
    '[shoe, 8000, "car_id":"asdlp-2394397-2njn84-kncbka", \'store\', "boat": 20, "price":4050, "age":43]'
]

您如何使用正则表达式获得以下列表?

car_id = ['128bhsdfb03-2389-1379be8', '', 'asdlp-2394397-2njn84-kncbka']

我尝试使用以下代码,但是没有用:

new_results = []

for item in car_id:
    new_list = re.search(r"car_id[^\d]*?(\d+)", item)
if new_list:
    new_results.append(str(item.group(1)))

1 个答案:

答案 0 :(得分:1)

使用re.findall

import re

Z100 = [ '[ship, "house", "boat": 65, "price":10000, "car_id":"128bhsdfb03-2389-1379be8", "age":15]', '[ ]', '[shoe, 8000, "car_id":"asdlp-2394397-2njn84-kncbka", "store", "boat": 20, "price":4050, "age":43]' ] 

lst = []
for x in Z100:
    v = re.findall(r'"car_id":"(.*?)"', x)  # Or re.findall(r'"car_id":"([^"]+)', x)
    if v:
        lst.extend(v)
    else:
        lst.extend(' ')

print(lst)
# ['128bhsdfb03-2389-1379be8', ' ', 'asdlp-2394397-2njn84-kncbka']