python - 使用 regex 删除首字母缩写词,基于括号后面的大写字符
问题描述
如何删除以下内容:
- 首字母缩写词以左括号开头,后跟大写字母或数字:例如“(ABC”或“(ABC)”或“(ABC-2A)”或“(ABC-1)”。
但不是括号之间的单词,以大写开头,后跟小写,例如“(鲍比)”或“(鲍勃去海滩……)”——>这是我正在努力解决的部分。
text = ['(ABC went to the beach', 'The girl (ABC-2A) is walking', 'The dog (Bobby) is being walked', 'They are there (ABC)' ]
for string in text:
cleaned_acronyms = re.sub(r'\([A-Z]*\)?', '', string)
print(cleaned_acronyms)
#current output:
>> 'went to the beach' #Correct
>>'The girl -2A) is walking' #Not correct
>>'The dog obby) is being walked' #Not correct
>>'They are there' #Correct
#desired & correct output:
>> 'went to the beach'
>>'The girl is walking'
>>'The dog (Bobby) is being walked' #(Bobby) is NOT an acronym (uppercase+lowercase)
>>'They are there'
解决方案
\([A-Z\-0-9]{2,}\)?
在以下上下文中使用:
import re
text = ['(ABC went to the beach', 'The girl (ABC-2A) is walking', 'The dog (Bobby) is being walked', 'They are there (ABC)' ]
for string in text:
cleaned_acronyms = re.sub(r'\([A-Z\-0-9]{2,}\)?', '', string)
print(cleaned_acronyms)
我得到这些结果:
' went to the beach'
'The girl is walking'
'The dog (Bobby) is being walked'
'They are there '
推荐阅读
- amazon-web-services - 通过 BITS 下载 AWS S3
- amazon-web-services - AWS 时间流数据库 - AWS 物联网
- react-native - 反应原生图像类型 .jfif 支持
- c# - 将 Blazor RenderFragment 参数转发给子组件会引发异常
- vue.js - 如何使用 Composition API 访问 Vuex 地图助手
- git - 如何根据 repo 状态打开和关闭 git 子模块
- gpu - 未检测到 nvidia gpu,甚至尝试从 BIOS 启用/禁用 igpu 和主显示器
- javascript - 从另一个文件中的另一个组件更改一个组件的状态
- c# - 是否可以在不离开应用程序的情况下使用按钮组织智能手机上所有通知的显示?
- python - (Python3) FLASK ADMIN:可以充分利用表格查看数据