面试题: 字符串a = "not 404 found 张三 99 深圳",每个词中间是空格,用正则过滤掉英文和数字,最终输出"张三 深圳"
常规思路:
先将字符串转变成列表,根据正则匹配到数字和英文字符,然后从列表中移除,用join重新拼接字符串
import re
S = "not 404 found 张三 99 深圳"
L = S.split(" ") # ['not', '404', 'found', '张三', '99', '深圳']
res = re.findall("\d+|[a-zA-Z]+", S) # ['not', '404', 'found', '99']
tmp = [item for item in L if not item in res] # ['张三', '深圳']
print(" ".join(tmp)) # 张三 深圳
逆向思路:
以英文字符和数字作为分隔符,得到一个空字符和中文的列表,再用join来拼接
import re
S = "not 404 found 张三 99 深圳"
L = re.split("[\d+|(a-z)+]", S) # ['', '', '', ' ', '', '', ' ', '', '', '', '', ' 张三 ', '', ' 深圳']
print("".join(L).strip()) # 张三 深圳
其他方法,比如:
1. re.sub过滤数字及字母
>>> import re
>>> S = "not 404 found 张三 99 深圳"
>>> " ".join(re.sub(r"[0-9a-zA-Z]", "", S).split())
'张三 深圳'
>>>
2. 直接按中文匹配[\u4e00-\u9fa5]
>>> import re
>>> S = "not 404 found 张三 99 深圳"
>>> " ".join(re.findall(r"[\u4e00-\u9fa5]+", S))
'张三 深圳'
>>>