Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract a certain substring from string using regex in python – dictionary value?

I have a str ‘data’ below contains some url content. using regex I want to only extract "url":"https://www.google.com". Here’s what I tried so far.

impor re
url = re.findall(‘"url":.*’, data)

Any help would be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{"__ref":"QXNzZXRDb250cmFjdFR5cGU6NDU0OTE3"},"decimals":null,"relayId":"QXNzZXRUeXBlOjE1MDQ5OTUxMQ==","favoritesCount":10,"isDelisted":false,"isFavorite":false,"isFrozen":false,"hasUnlockableContent":false,"tokenId":"3379","collection":{"__ref":"Q29sbGVjdGlvblR5cGU6MzY3NDA5MQ=="},"orderData":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:orderData"},"name":"Flame River Chumbi","authenticityMetadata":null,"imageUrl":"https:\u002F\u002Flh3.googleusercontent.com\u002FtM1wwmVb1dA5DadG-QXFBd1nWnqi6DRh7fL0nHgjr2eZkSMKIhcPcwSuBLEvRIgOQRuFDKiovy1kpHSGz4NciKw6oSON-_gdz8aXKw","creator":null,"assetOwners(first:1)":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:assetOwners(first:1)"},"animationUrl":null,"backgroundColor":null,"description":"Chumbi are adorable bipedal creatures that inhabit a lush and mysterious forest valley. They have a spiritual connection to the forest and use magical spells to maintain and protect it.","isListable":true,"isReportedSuspicious":false,"traits(first:100)":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:traits(first:100)"},"displayImageUrl":"https:\u002F\u002Flh3.googleusercontent.com\u002FtM1wwmVb1dA5DadG-QXFBd1nWnqi6DRh7fL0nHgjr2eZkSMKIhcPcwSuBLEvRIgOQRuFDKiovy1kpHSGz4NciKw6oSON-_gdz8aXKw","externalLink":null,"isEditableByOwner":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:isEditableByOwner"}, "url":"https:://google.com" ,"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:traits(first:100)","displayImageUrl":"https:\u002F\u002Flh3.googleusercontent.com\u002FtM1wwmVb1dA5DadG-QXFBd1nWnqi6DRh7fL0nHgjr2eZkSMKIhcPcwSuBLEvRIgOQRuFDKiovy1kpHSGz4NciKw6oSON-_gdz8aXKw","externalLink":null,"isEditableByOwner":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:isEditableByOwner"} 

>Solution :

What you want to know is non-greedy search.
Try the following regex:

import re
s = '{"__ref":"QXNzZXRDb250cmFjdFR5cGU6NDU0OTE3"},"decimals":null,"relayId":"QXNzZXRUeXBlOjE1MDQ5OTUxMQ==","favoritesCount":10,"isDelisted":false,"isFavorite":false,"isFrozen":false,"hasUnlockableContent":false,"tokenId":"3379","collection":{"__ref":"Q29sbGVjdGlvblR5cGU6MzY3NDA5MQ=="},"orderData":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:orderData"},"name":"Flame River Chumbi","authenticityMetadata":null,"imageUrl":"https:\u002F\u002Flh3.googleusercontent.com\u002FtM1wwmVb1dA5DadG-QXFBd1nWnqi6DRh7fL0nHgjr2eZkSMKIhcPcwSuBLEvRIgOQRuFDKiovy1kpHSGz4NciKw6oSON-_gdz8aXKw","creator":null,"assetOwners(first:1)":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:assetOwners(first:1)"},"animationUrl":null,"backgroundColor":null,"description":"Chumbi are adorable bipedal creatures that inhabit a lush and mysterious forest valley. They have a spiritual connection to the forest and use magical spells to maintain and protect it.","isListable":true,"isReportedSuspicious":false,"traits(first:100)":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:traits(first:100)"},"displayImageUrl":"https:\u002F\u002Flh3.googleusercontent.com\u002FtM1wwmVb1dA5DadG-QXFBd1nWnqi6DRh7fL0nHgjr2eZkSMKIhcPcwSuBLEvRIgOQRuFDKiovy1kpHSGz4NciKw6oSON-_gdz8aXKw","externalLink":null,"isEditableByOwner":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:isEditableByOwner"}, "url":"https:://google.com" ,"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:traits(first:100)","displayImageUrl":"https:\u002F\u002Flh3.googleusercontent.com\u002FtM1wwmVb1dA5DadG-QXFBd1nWnqi6DRh7fL0nHgjr2eZkSMKIhcPcwSuBLEvRIgOQRuFDKiovy1kpHSGz4NciKw6oSON-_gdz8aXKw","externalLink":null,"isEditableByOwner":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:isEditableByOwner"}'
results = re.findall(r'"url":".*?"', s)

Answer to the OP’s comment:

You can use grouping.

import re
s = '{"__ref":"QXNzZXRDb250cmFjdFR5cGU6NDU0OTE3"},"decimals":null,"relayId":"QXNzZXRUeXBlOjE1MDQ5OTUxMQ==","favoritesCount":10,"isDelisted":false,"isFavorite":false,"isFrozen":false,"hasUnlockableContent":false,"tokenId":"3379","collection":{"__ref":"Q29sbGVjdGlvblR5cGU6MzY3NDA5MQ=="},"orderData":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:orderData"},"name":"Flame River Chumbi","authenticityMetadata":null,"imageUrl":"https:\u002F\u002Flh3.googleusercontent.com\u002FtM1wwmVb1dA5DadG-QXFBd1nWnqi6DRh7fL0nHgjr2eZkSMKIhcPcwSuBLEvRIgOQRuFDKiovy1kpHSGz4NciKw6oSON-_gdz8aXKw","creator":null,"assetOwners(first:1)":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:assetOwners(first:1)"},"animationUrl":null,"backgroundColor":null,"description":"Chumbi are adorable bipedal creatures that inhabit a lush and mysterious forest valley. They have a spiritual connection to the forest and use magical spells to maintain and protect it.","isListable":true,"isReportedSuspicious":false,"traits(first:100)":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:traits(first:100)"},"displayImageUrl":"https:\u002F\u002Flh3.googleusercontent.com\u002FtM1wwmVb1dA5DadG-QXFBd1nWnqi6DRh7fL0nHgjr2eZkSMKIhcPcwSuBLEvRIgOQRuFDKiovy1kpHSGz4NciKw6oSON-_gdz8aXKw","externalLink":null,"isEditableByOwner":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:isEditableByOwner"}, "url":"https:://google.com" ,"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:traits(first:100)","displayImageUrl":"https:\u002F\u002Flh3.googleusercontent.com\u002FtM1wwmVb1dA5DadG-QXFBd1nWnqi6DRh7fL0nHgjr2eZkSMKIhcPcwSuBLEvRIgOQRuFDKiovy1kpHSGz4NciKw6oSON-_gdz8aXKw","externalLink":null,"isEditableByOwner":{"__ref":"client:QXNzZXRUeXBlOjE1MDQ5OTUxMQ==:isEditableByOwner"}'
results = re.findall(r'"url":"(.*?)"', s)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading