我目前的代码如下:
idk = {"id":30511879634,"title":"3.5y","option1":"3.5y","option2":null,"option3":null,"sku":"","requires_shipping":true,"taxable":true,"featured_image":{"id":18778730002,"product_id":8876555346,"position":1,"created_at":"2017-02-15T15:51:03-05:00","updated_at":"2017-02-15T15:51:37-05:00","src":"https:\/\/cdn.shopify.com\/s\/files\/1\/1527\/4931\/products\/AJ6_HEIRESS_PRODUCT.jpg?v=1487191897","variant_ids":[30511879634,30511879698,30511879762,30511879826,30511879890,30511879954,30511880018,30511880082]},"available":false,"name":"Air Jordan 6 Retro Premium GG 'Heiress' - 3.5y","public_title":"3.5y","options":["3.5y"],"price":16000,"weight":1361,"compare_at_price":null,"inventory_quantity":0,"inventory_management":"shopify","inventory_policy":"deny","barcode":""},{"id":30511879698,"title":"4y","option1":"4y","option2":null,"option3":null,"sku":"","requires_shipping":true,"taxable":true,"featured_image":{"id":18778730002,"product_id":8876555346,"position":1,"created_at":"2017-02-15T15:51:03-05:00","updated_at":"2017-02-15T15:51:37-05:00","src":"https:\/\/cdn.shopify.com\/s\/files\/1\/1527\/4931\/products\/AJ6_HEIRESS_PRODUCT.jpg?v=1487191897","variant_ids":[30511879634,30511879698,30511879762,30511879826,30511879890,30511879954,30511880018,30511880082]},"available":true,"name":"Air Jordan 6 Retro Premium GG 'Heiress' - 4y","public_title":"4y","options":["4y"],"price":16000,"weight":1361,"compare_at_price":null,"inventory_quantity":1,"inventory_management":"shopify","inventory_policy":"deny","barcode":""},
variants = re.findall(r'"id":(.*?),"title"', idk)
返回['30511879634', '18778730002,"product_id":8876555346,"position":1,"created_at":"2017-02-15T15:51:03-05:00","updated_at":"2017-02-15T15:51:37-05:00","src":"https:\\/\\/cdn.shopify.com\\/s\\/files\\/1\\/1527\\/4931\\/products\\/AJ6_HEIRESS_PRODUCT.jpg?v=1487191897","variant_ids":[30511879634,30511879698,30511879762,30511879826,30511879890,30511879954,30511880018,30511880082]},"available":false,"name":"Air Jordan 6 Retro Premium GG \'Heiress\' - 3.5y","public_title":"3.5y","options":["3.5y"],"price":16000,"weight":1361,"compare_at_price":null,"inventory_quantity":0,"inventory_management":"shopify","inventory_policy":"deny","barcode":""},{"id":30511879698']
但我希望它返回['30511879634', '30511879698']
我知道我可以variants = re.findall(r'"id":[^"product_id"].,"title"', idk)
但是会返回['"id":30511879634,"title"', '"id":30511879698,"title"']
我已经尝试variants = re.findall(r'"id":[^"product_id"](.*?),"title"', idk)
但这不起作用。无论如何我只能返回数字,同时确保第二个ID(18778730002)不包含在列表中,而只是30511879634和30511879698。
答案 0 :(得分:-1)
您可以使用此正则表达式 ...
(?<=\"id\":)\d+(?=,\"title\")
python (demo)
import re
idk = """{"id":30511879634,"title":"3.5y","option1":"3.5y","option2":null,"option3":null,"sku":"","requires_shipping":true,"taxable":true,"featured_image":{"id":18778730002,"product_id":8876555346,"position":1,"created_at":"2017-02-15T15:51:03-05:00","updated_at":"2017-02-15T15:51:37-05:00","src":"https:\/\/cdn.shopify.com\/s\/files\/1\/1527\/4931\/products\/AJ6_HEIRESS_PRODUCT.jpg?v=1487191897","variant_ids":[30511879634,30511879698,30511879762,30511879826,30511879890,30511879954,30511880018,30511880082]},"available":false,"name":"Air Jordan 6 Retro Premium GG 'Heiress' - 3.5y","public_title":"3.5y","options":["3.5y"],"price":16000,"weight":1361,"compare_at_price":null,"inventory_quantity":0,"inventory_management":"shopify","inventory_policy":"deny","barcode":""},{"id":30511879698,"title":"4y","option1":"4y","option2":null,"option3":null,"sku":"","requires_shipping":true,"taxable":true,"featured_image":{"id":18778730002,"product_id":8876555346,"position":1,"created_at":"2017-02-15T15:51:03-05:00","updated_at":"2017-02-15T15:51:37-05:00","src":"https:\/\/cdn.shopify.com\/s\/files\/1\/1527\/4931\/products\/AJ6_HEIRESS_PRODUCT.jpg?v=1487191897","variant_ids":[30511879634,30511879698,30511879762,30511879826,30511879890,30511879954,30511880018,30511880082]},"available":true,"name":"Air Jordan 6 Retro Premium GG 'Heiress' - 4y","public_title":"4y","options":["4y"],"price":16000,"weight":1361,"compare_at_price":null,"inventory_quantity":1,"inventory_management":"shopify","inventory_policy":"deny","barcode":""}"""
variants = re.findall(r"(?<=\"id\":)\d+(?=,\"title\")", idk)
print(variants) #-> ['30511879634', '30511879698']