Python 用于删除除A-z以外的所有字符和删除所有包含数字的字的正则表达式

Python 用于删除除A-z以外的所有字符和删除所有包含数字的字的正则表达式,python,regex,replace,Python,Regex,Replace,我的目标是编写一个函数,该函数输入文本并用空格替换除拉丁字母(a-z)以外的所有字符,此外还删除所有包含数字的单词。然后用一个空格替换所有多个空格 例如: ' hello, world! ho1hoho2ho, merry xmas!! ho1ho1 :))' -> 'hello world merry xmas'. 实现此功能的Python函数: def clean_text(text): text_valid = re.sub(u'[^A-z0-9]', ' ', text

我的目标是编写一个函数,该函数输入文本并用空格替换除拉丁字母(a-z)以外的所有字符,此外还删除所有包含数字的单词。然后用一个空格替换所有多个空格

例如:

' hello, world! ho1hoho2ho, merry xmas!! ho1ho1 :))' -> 'hello world merry xmas'. 
实现此功能的Python函数:

def clean_text(text):
    text_valid = re.sub(u'[^A-z0-9]', ' ', text)
    return ' '.join(word for word in text_valid.split()
                    if not re.search(r'\d', word))
现在我想知道是否有一个正则表达式,也许,所以我可以写一些类似的东西

return ' '.join(re.findall(enter_my_magical_regex_here))

或者,也许有另一种方法可以用更快(希望更短)的代码替换上面的代码。

这将为您提供所需的输出-

x = ' hello, world! ho1hoho2ho, merry xmas!! ho1ho1 :))'
re.sub('[!,]', '', ' '.join([i for i in x.split() if not re.findall('[\d+:\\?\"<>*/|]', i)]))
x='你好,世界!呵呵,圣诞快乐!!ho1ho1:)'
re.sub(“[!,]”,“,”,“,”).join([i代表x.split()中的i,如果不是re.findall(“[\d+:\ \?\”*/;]”,i)])

但是你可能需要在这里和那里调整一些东西

这将得到你想要的输出-

x = ' hello, world! ho1hoho2ho, merry xmas!! ho1ho1 :))'
re.sub('[!,]', '', ' '.join([i for i in x.split() if not re.findall('[\d+:\\?\"<>*/|]', i)]))
x='你好,世界!圣诞快乐!!圣诞快乐:)'
re.sub(“[!,]”,“,”,“,”).join([i代表x.split()中的i,如果不是re.findall(“[\d+:\ \?\”*/;]”,i)])
但是你可能需要在这里和那里调整一些东西

你可以使用

' '.join(re.sub('([^A-Za-z0-9 ]|[^ ]*[0-9][^ ]*)', '', text).split())
你可以用

' '.join(re.sub('([^A-Za-z0-9 ]|[^ ]*[0-9][^ ]*)', '', text).split())

仅供参考:该字符集应为
[^0-9A-Za-z]
,以匹配除ASCII数字和字母以外的所有字符。(
\W
会让下划线通过。)不要使用
[A-а]
匹配俄语字母,您需要使用
[А-а-ёЁ]
。英文字符可以与
[a-zA-Z]
匹配。请尝试
re.sub(ur'^\s*.$$$\s*(?:[^\W\d\U]*\d[^\W\d\U]*.[^a-zA-Z\d\s]+,'',text,flags=re.U)
请参考:该字符集应为
[^0-9A-zA-Z]
,以匹配ASCII数字和字母以外的所有字符。(
\W
会让下划线通过。)不要使用
[A-а]
匹配俄语字母,您需要使用
[А-а-ёЁ]
。英语单词可以与
[a-zA-Z]
匹配。试试
re.sub(ur'^\s*.$$\s*(?:[^\W\d\U]*\d[^\W\d\U]*.[^a-zA-Z\d\s]+,'',text,flags=re.U)
看看
U
的用法是什么?