问题描述
我的目标是识别出现在@PROG$ 之后的缩写词并将其更改为@PROG$。 (例如 ALI -> @PROG$)
输入
s = "背景(未签名):我们之前的研究表明,@PROG$ (ALI) 和 C 反应蛋白 (CRP) 是可手术非小细胞肺癌 (NSCLC) 患者的独立重要预后因素。"
“背景(未签名):我们之前的研究表明,@PROG$、@PROG$ 和 C 反应蛋白 (CRP) 是可手术非小细胞肺癌 (NSCLC) 患者的独立重要预后因素。”
>我尝试了这样的re.findall('(\(.*?\))',s)
,它给了我所有的缩写。有什么帮助吗?我需要修复什么?
解决方法
您可以使用 re.sub
解决方案,例如
import re
s = "Background (UNASSIGNED): Previous study of ours showed that @PROG$ (ALI) and C-reactive protein (CRP) are independent significant prognostic factors in operable non-small cell lung cancer (NSCLC) patients."
print( re.sub(r'(@PROG\$\s+)\([A-Z]+\)',r'\1@PROG$',s) )
# => Background (UNASSIGNED): Previous study of ours showed that @PROG$ @PROG$ and C-reactive protein (CRP) are independent significant prognostic factors in operable non-small cell lung cancer (NSCLC) patients.
参见Python demo。正则表达式是
(@PROG\$\s+)\([A-Z]+\)
参见regex demo。详情:
-
(@PROG\$\s+)
- 第 1 组(\1
指的是替换模式中的该组值):@PROG$
和一个或多个空格 -
\(
-(
字符 -
[A-Z]+
- 一个或多个大写 ASCII 字母(替换为[^()]*
以匹配括号之间的任何内容,除了(
和)
) -
\)
- 一个)
字符。