使用正则表达式查找特定单词后面的缩写

2024-05-20 13:43:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是识别出现在@PROG$后面的缩写词,并将其更改为@PROG$。(如阿里->;@PROG$)

输入

s=“背景(未指定):我们之前的研究表明@PROG$(ALI)和C反应蛋白(CRP)是可手术非小细胞肺癌(NSCLC)患者的独立重要预后因素。”

输出

“背景(未指定):我们之前的研究表明,@PROG$@PROG$和C反应蛋白(CRP)是可手术非小细胞肺癌(NSCLC)患者的独立重要预后因素。”

我试过这样的东西,它给了我所有的缩写。这里有什么帮助吗?我需要修什么


Tags: gt患者目标ali细胞手术因素背景
1条回答
网友
1楼 · 发布于 2024-05-20 13:43:14

您可以使用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+)-group1(\1指替换模式中的该组值):@PROG$和一个或多个空格
  • \(-a(字符
  • [A-Z]+-一个或多个大写ASCII字母(替换为[^()]*,以匹配括号中除()之外的任何内容)
  • \)-a)字符

相关问题 更多 >