the Pythoner
the Pythoner

Reputation: 15

How to get a substring of this string Python3

string:

ecsdcsdcsdfvdfv":"https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C","vsvdfvsfvcfvfdcvfd

substring that i want to get:

https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C

i tried this but doesn't work

print (log.split("ecsdcsdcsdfvdfv",1)[1]) 

Upvotes: 0

Views: 54

Answers (3)

Henry Harutyunyan
Henry Harutyunyan

Reputation: 2415

If you want to get the string between the ":" and ",", then you can use regular expression to do it.

re.match('(.*\":\")([^\",\"]*)(\",\".*)', log).group(2)

Given your input

'ecsdcsdcsdfvdfv":"https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C","vsvdfvsfvcfvfdcvfd'

you will get

'https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C'

And if you input something like

'ecsdcsdcs":"dfvdfv":"https://s...F0A0C","vsvdfvsfvcfvfdc","vfd'

you will get

'https://s...F0A0C'

Dont forget to import re.

Upvotes: 1

Guimoute
Guimoute

Reputation: 4629

This yield the expected result.

The way you used .split() was wrong, you did not include the " character.

string = 'ecsdcsdcsdfvdfv":"https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C","vsvdfvsfvcfvfdcvfd'
substring = string.split('ecsdcsdcsdfvdfv":"')[1].split('","vsvdfvsfvcfvfdcvfd')[0]

Upvotes: 2

Henry Harutyunyan
Henry Harutyunyan

Reputation: 2415

You can try this

log.split('":"')[1].split('","')[0]

But this is not the best way to do what you are trying to achieve. Better parse it and get what you want.

Upvotes: 2

Related Questions