【发布时间】:2021-01-22 19:01:10
【问题描述】:
我想删除此字符串中每个大写单词和数字开头的不需要的 r 和 n。我试过正则表达式。不确定正则表达式或其他方法在这里是否有用。
这是我尝试使用的代码:
text = "nFamily n49 new nTom"
regex_pattern = re.compile(r'.*n[A-Z][a-z]*|[0-9]*\s')
matches = regex_pattern.findall(text)
for match in matches:
text = text.replace(match," ")
print(text)
预期输出:
Family 49 new Tom
【问题讨论】:
-
使用
re.sub(r'\b[rn](?=[A-Z\d])', "", text)
标签: python python-3.x regex data-extraction