最近处理文本,使用正则表达式去除文本中的各种标点符号,但是保留英文间的空格,包括转义符号在hive -e和python中的区别实验,总结如下两点,以求备份:
Hive -e 中使用正则:
hive -e "select regexp_replace(
regexp_replace(
regexp_replace(lower(trim(' 美女 主播# skin a skin and IPhone 11')),'([\\\u4e00-\\\u9fa5]) * ([\\\u4e00-\\\u9fa5])', '\\\$1\\\$2'),
'([a-zA-Z]+) * ([0-9]+)', '\\\$1\\\$2'),'[#]+', '')"
Python 中使用:
def _str_normalization(self, text):
# 以下去除顺序不可变
# 去开头空格和中文间的空格
pattern_clean = u"([\u4e00-\u9fa5]) * ([\u4e00-\u9fa5])"
text_clean = re.sub(pattern_clean, r"\1\2", text.strip())
# 去掉 英文和数字间的空格
pattern_clean = u"([a-zA-Z]+) * ([0-9]+)"
text_clean = re.sub(pattern_clean, r"\1\2", text_clean)
# 去掉标点符号,不去数字
pattern_clean = u"[#.,,?!/。?!@<>《》+=~-……【】{}]+"
text_clean = re.sub(pattern_clean, u"", text_clean.lower())
return text_clean