假设我有一个句子,其中包含单词、整数和它们的组合:
"This is a string with an integer 1 and a 2 and a 3 and a 1A69 and a 1B and a C3"
是否有可能从由字母和数字组成的单词中删除所有整数?例如,我希望上述内容变为
"This is a string with an integer 1 and a 2 and a 3 and a A and a B and a C"
一种选择是移除在字母前面(使用零宽度正回顾后缀 ?<=
语法)或者后面(使用零宽度正预测后缀 ?=
语法)的数字:
import re
s = "This is a string with an integer 1 and a 2 and a 3 and a 1A69 and a 1B and a C3"
re.sub(r'\d+(?=[a-zA-Z])|(?<=[a-zA-Z])\d+', '', s)
# 'This is a string with an integer 1 and a 2 and a 3 and a A and a B and a C'
def remove_digits(s):
return ''.join([x for x in s if not x.isdigit()])
def is_combined(s):
return any(x.isalpha() for x in s) and any(x.isdigit() for x in s)
' '.join([remove_digits(x) if is_combined(x) else x for x in test.split()])