python中正则表达式的修改

2024-04-25 09:21:49 发布

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

我使用下面的regex模式来识别缩写。你知道吗

mytext = "This is AVGs and (NMN) and most importantly GFD"
mytext= re.sub(r"\b[A-Z\.]{2,}s?\b", "_ABB", mytext)
print(mytext)

我得到如下输出。你知道吗

This is _ABB and (_ABB) and most importantly _ABB

但是,我想得到输出为

This is AVGs_ABB and (NMN_ABB) and most importantly GFD_ABB

请告诉我哪里做错了。你知道吗


Tags: andremostis模式thisregexprint
3条回答

使用捕获组捕获要匹配的单词边界之间的模式,然后在替换中使用它。第一个捕获组将作为\\1提供。你知道吗

mytext = "This is AVGs and (NMN) and most importantly GFD"
mytext= re.sub(r"\b([A-Z\.]{2,}s?)\b", "\\1_ABB", mytext)
print(mytext)

Demo of code snippet

替换时使用排除,如下所示:

import re 
mytext = "This is AVGs and (NMN) and most importantly GFD"
mytext= re.sub(r"([A-Z]{2,})", "\\1_ABB", mytext)
print(mytext)

输出:

This is AVGs_ABB and (NMN_ABB) and most importantly GFD_ABB

试试这个

In [1]: str = "This is AVGs and (NMN) and most importantly GFD"
In [2]: regex = "[A-Z]{2,}"
In [3]: import re
In [4]: result = re.sub(regex, "_ABB", str)
In [5]: result
Out[5]: 'This is _ABBs and (_ABB) and most importantly _ABB'

相关问题 更多 >