简体   繁体   中英

How can I capture the dynamic text from regex in Python?

import re
see = """https://www.flipkart.com/honor-7c-blue-64-gb/p/itmfaybh5jjzhmpz?pid=MOBF3T87CECXMZQH&lid=LSTMOBF3T87CECXMZQHFPGOZQ&marketplace=FLIPKART","name":"Honor 7C (Blue, 64 GB)"},{"@type":"ListItem","position":2,"url":"https://www.flipkart.com/honor-7c-blue-32-gb/p/itmf53qmmggh2ynj?pid=MOBF3T87QFSQXGCM&lid=LSTMOBF3T87QFSQXGCMUHVDHM&marketplace=FLIPKART","name":"Honor 7C (Blue, 32 GB)"},{"@type":"ListItem","position":3,"url":"https://www.flipkart.com/honor-7c-black-32-gb/p/itmf5ch2zb97avvz?pid=MOBF3T877EEKFWGN&lid=LSTMOBF3T877EEKFWGN9WQ3A4&marketplace=FLIPKART","name":"Honor 7C (Black, 32 GB)"},{"@type":"ListItem","position":4,"url":"https://www.flipkart.com/honor-8c-black-64-gb/p/itmfc8c4fsekrpdp?pid=MOBFC8C8FXXNHZ7C&lid=LSTMOBFC8C8FXXNHZ7CZYQGKP&marketplace=FLIPKART"""
regex = r'\"position":2,\"url\":\",\"name\":\"(.*)\"'
match = re.findall(regex, see)

print(match)

I want to capture just the names from the above variable the output must be in this way - (Honor 7C (Blue, 64 GB),Honor 7C (Blue, 32 GB),Honor 7C (Black, 32 GB),)

while i run the above code it just gives me empty parenthesis

I think so that something is wrong with my regex so anyone please help me

THANKING IN ADVANCE.

Do you mean this? This will look for the "name" and take everything within the next quotes as the name regex group.

(.*?) means everything until the first occurrence of " .

import re

see = """https://www.flipkart.com/honor-7c-blue-64-gb/p/itmfaybh5jjzhmpz?pid=MOBF3T87CECXMZQH&lid=LSTMOBF3T87CECXMZQHFPGOZQ&marketplace=FLIPKART","name":"Honor 7C (Blue, 64 GB)"},{"@type":"ListItem","position":2,"url":"https://www.flipkart.com/honor-7c-blue-32-gb/p/itmf53qmmggh2ynj?pid=MOBF3T87QFSQXGCM&lid=LSTMOBF3T87QFSQXGCMUHVDHM&marketplace=FLIPKART","name":"Honor 7C (Blue, 32 GB)"},{"@type":"ListItem","position":3,"url":"https://www.flipkart.com/honor-7c-black-32-gb/p/itmf5ch2zb97avvz?pid=MOBF3T877EEKFWGN&lid=LSTMOBF3T877EEKFWGN9WQ3A4&marketplace=FLIPKART","name":"Honor 7C (Black, 32 GB)"},{"@type":"ListItem","position":4,"url":"https://www.flipkart.com/honor-8c-black-64-gb/p/itmfc8c4fsekrpdp?pid=MOBFC8C8FXXNHZ7C&lid=LSTMOBFC8C8FXXNHZ7CZYQGKP&marketplace=FLIPKART"""
regex = r'\"name\":\"(?P<name>.*?)\"'
print(re.findall(regex, see))
# ['Honor 7C (Blue, 64 GB)', 'Honor 7C (Blue, 32 GB)', 'Honor 7C (Black, 32 GB)']

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