簡體   English   中英

Python re.findall在兩個字符串之間,同時排除字符串

[英]Python re.findall between two strings while excluding strings

我當前的代碼如下:

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。

您可以使用此正則表達式 ...

(?<=\"id\":)\d+(?=,\"title\")

參見正則表達式演示/說明

python演示

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM