【发布时间】:2021-07-26 18:53:58
【问题描述】:
如何删除以下内容:
- 首字母缩写词以左括号开头,后跟大写或 号码:例如'(ABC' 或 '(ABC)' 或 '(ABC-2A)' 或 '(ABC-1)'。
但NOT括号之间的单词以大写开头,后跟小写,例如'(Bobby)' 或 '(Bob went to the beach..)' --> 这是我正在努力解决的部分。
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'
【问题讨论】:
标签: python regex string uppercase re