Home > database >  How to select specified area in Python text
How to select specified area in Python text

Time:06-08

I want to select only cuttly link area ("https://cutt.ly/XJDV1G8") I tried RegEx but didin't make it happen. Below code, I tried to select ("") area but I need to select 8th of the ("") areas.

import re

text = '{"url":{"status":7,"fullLink":"https:\/\/twitter.com\/furkan_alkaya_","date":"2022-06-08","shortLink":"https:\/\/cutt.ly\/XJDV1G8","title":"Furkan Alkaya (@furkan_alkaya_) | Twitter"}}'

x = re.search(r"\b""\w ",text)

print(x.group())

CodePudding user response:

What you have is JSON, so you should use the JSON library to process it:

import json

text = '{"url":{"status":7,"fullLink":"https:\/\/twitter.com\/furkan_alkaya_","date":"2022-06-08","shortLink":"https:\/\/cutt.ly\/XJDV1G8","title":"Furkan Alkaya (@furkan_alkaya_) | Twitter"}}'
d = json.loads(text)
d['url']['shortLink']

Output:

'https://cutt.ly/XJDV1G8'

CodePudding user response:

using the regex:

x = re.search(r'("shortLink":")(\w{5}:.{20})', text)
x[2]

CodePudding user response:

this will select the 8th of ("...") includes empty (""):

match = re.search(r'(?:"([^"]*)". ?){8}', text)
value = match[1] if match else None

print( value )
  • Related