从单词中删除数字

3

假设我有一个句子,其中包含单词、整数和它们的组合:

"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"

1
你应该使用正则表达式! - marcuse
2个回答

3

一种选择是移除在字母前面(使用零宽度正回顾后缀 ?<= 语法)或者后面(使用零宽度正预测后缀 ?= 语法)的数字:

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'

1
或者,不使用正则表达式:

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()])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接