从字符串中删除与字母混合的数字

PJa*_*Jay -7 python regex string

假设我有一个字符串,例如:

string = 'This string 22 is not yet perfect1234 and 123pretty but it can be.'
Run Code Online (Sandbox Code Playgroud)

我想从我的字符串中删除任何与单词混合的数字,例如'perfect1234'and '123pretty'但不是 '22',并获得如下输出:

string = 'This string 22 is not yet perfect and pretty but it can be.'
Run Code Online (Sandbox Code Playgroud)

有没有办法使用正则表达式或任何其他方法在 Python 中做到这一点?任何帮助,将不胜感激。谢谢!

dan*_*dar 5

s = 'This string 22 is not yet perfect1234 and 123pretty but it can be.'

new_s = ""
for word in s.split(' '):
    if any(char.isdigit() for char in word) and any(c.isalpha() for c in word):
        new_s += ''.join([i for i in word if not i.isdigit()])
    else:
        new_s += word
    new_s += ' '
Run Code Online (Sandbox Code Playgroud)

结果:

'This string 22 is not yet perfect and pretty but it can be.'
Run Code Online (Sandbox Code Playgroud)


Keo*_*zon 5

如果你想保留数字本身(不是包含字母字符的单词的一部分),这个正则表达式可以完成这项工作(但可能有一种方法可以让它更简单):

import re
pattern = re.compile(r"\d*([^\d\W]+)\d*")
s = "This string is not yet perfect1234 and 123pretty but it can be. 45 is just a number."
pattern.sub(r"\1", s)
'This string is not yet perfect and pretty but it can be. 45 is just a number.'
Run Code Online (Sandbox Code Playgroud)

在这里,剩下 45 是因为它不是单词的一部分。