如果存在特定单词,则从括号中删除文本
Removing text from brackets if particular word exist
我有两个字符串,如果括号内存在某个单词,则尝试删除括号内的文本,对于 s1,我可以通过使用下面的正则表达式获得所需的输出,但对于 s2,相同的逻辑不起作用。不知道gng怎么了。
s1 = '((Xyz_lk eou ) some stuff (XYZ_l))) (vc vbn XyZ no.s)'
s2 = '( 100 % eou ) - 1h-imidazole 5,5- [ 1,1 -biphenyl ] -4,4 -diylbis [ 2 -- 2-pyrrolidinyl ] hydrochloride'
逻辑:
vc1 = re.sub(r"\s*\((\w|\s)*(eou|eou)(\w|\s)*\)", "", s1)
print(vc1)
vc2 = re.sub(r"\s*\((\w|\s)*(eou|eou)(\w|\s)*\)", "", s2)
print(vc2)
输出
vc1: (some stuff (XYZ_l))) (vc vbn XyZ no.s)
vc2: ( 100 % eou ) - 1h-imidazole 5,5- [ 1,1 -biphenyl ] -4,4 -diylbis [ 2 -- 2-pyrrolidinyl ] hydrochloride
这是否解决了您的问题?
vc1 = re.sub(r"\([^())]*eou[^)]*\)", "", s1)
print(vc1)
==> ( some stuff (XYZ_l))) (vc vbn XyZ no.s)
vc1 = re.sub(r"\([^())]*eou[^)]*\)", "", s2)
print(vc1)
==> - 1h-imidazole 5,5- [ 1,1 -biphenyl ] -4,4 -diylbis [ 2 -- 2-pyrrolidinyl ] hydrochloride
我有两个字符串,如果括号内存在某个单词,则尝试删除括号内的文本,对于 s1,我可以通过使用下面的正则表达式获得所需的输出,但对于 s2,相同的逻辑不起作用。不知道gng怎么了。
s1 = '((Xyz_lk eou ) some stuff (XYZ_l))) (vc vbn XyZ no.s)'
s2 = '( 100 % eou ) - 1h-imidazole 5,5- [ 1,1 -biphenyl ] -4,4 -diylbis [ 2 -- 2-pyrrolidinyl ] hydrochloride'
逻辑:
vc1 = re.sub(r"\s*\((\w|\s)*(eou|eou)(\w|\s)*\)", "", s1)
print(vc1)
vc2 = re.sub(r"\s*\((\w|\s)*(eou|eou)(\w|\s)*\)", "", s2)
print(vc2)
输出
vc1: (some stuff (XYZ_l))) (vc vbn XyZ no.s)
vc2: ( 100 % eou ) - 1h-imidazole 5,5- [ 1,1 -biphenyl ] -4,4 -diylbis [ 2 -- 2-pyrrolidinyl ] hydrochloride
这是否解决了您的问题?
vc1 = re.sub(r"\([^())]*eou[^)]*\)", "", s1)
print(vc1)
==> ( some stuff (XYZ_l))) (vc vbn XyZ no.s)
vc1 = re.sub(r"\([^())]*eou[^)]*\)", "", s2)
print(vc1)
==> - 1h-imidazole 5,5- [ 1,1 -biphenyl ] -4,4 -diylbis [ 2 -- 2-pyrrolidinyl ] hydrochloride