我已经尝试过 re.search 但不知道正则表达式的确切语法来替换(介于两者之间的任何内容)。假设有以下字符串:
D08838 Acotiamide hydrochloride hydrate (JAN/BBC); Acotiamide hydrochloride (USAN) Antagonist
我想得到这样的东西:
D08838 Acotiamide hydrochloride hydrate; Acotiamide hydrochloride Antagonist
我会去:
import re
s = 'D08838 Acotiamide hydrochloride hydrate (JAN/BBC); Acotiamide hydrochloride (USAN) Antagonist'
print re.sub('(\s*\(.*?\))', '', s)
#D08838 Acotiamide hydrochloride hydrate; Acotiamide hydrochloride Antagonist
这个怎么样:
>>> import re
>>> s = "D08838 Acotiamide hydrochloride hydrate (JAN/BBC); Acotiamide hydrochloride (USAN) Antagonist"
>>> re.sub('\s\([\w/]+\)', '', s)
'D08838 Acotiamide hydrochloride hydrate; Acotiamide hydrochloride Antagonist'