解析具有[值](类型)格式的句子

sid*_*491 3 python regex

我想从给定的句子中解析和提取键,其值遵循以下格式:
I want to get [samsung](brand) within [1 week](duration) to be happy.

我想将其转换为如下所示的拆分列表:
['I want to get ', 'samsung:brand', ' within ', '1 week:duration', ' to be happy.']

我试图使用[或拆分它)

re.split('\[|\]|\(|\)',s)
Run Code Online (Sandbox Code Playgroud)

这给出了输出:

['I want to get ',
 'samsung',
 '',
 'brand',
 ' within ',
 '1 week',
 '',
 'duration',
 ' to be happy.']
Run Code Online (Sandbox Code Playgroud)

re.split('\[||\]|\(|\)',s)
Run Code Online (Sandbox Code Playgroud)

给下面的输出:

['I want to get ', 
'samsung](brand) within ', 
'1 week](duration) to be happy.']
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏。

注意:这类似于stackoverflow内联链接,如果我们键入:go to [this link](http://google.com)它将解析为链接。

And*_*ely 5

第一步,我们分割字符串,第二步,我们修改字符串:

s = 'I want to get [samsung](brand) within [1 week](duration) to be happy.'

import re

s = re.split('(\[[^]]*\]\([^)]*\))', s)
s = [re.sub('\[([^]]*)\]\(([^)]*)\)', r'\1:\2', i) for i in s]

print(s)
Run Code Online (Sandbox Code Playgroud)

印刷品:

['I want to get ', 'samsung:brand', ' within ', '1 week:duration', ' to be happy.']
Run Code Online (Sandbox Code Playgroud)