正则表达式替换so spark dataframe字符串列中的点字符 [英] Regex replace dot characters in a so spark dataframe string column

查看:315
本文介绍了正则表达式替换so spark dataframe字符串列中的点字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试替换句号。时在Spark(Scala)的一列中使用-将其替换为-

When I am trying to Replace the Full stop "." with "-" in a column in Spark(Scala) It replaces all the characters with "-"

val df3 = df2.withColumn("Current Ver", regexp_replace(col("Current Ver"), "." ,"-"))
df3.show()

例如-如果某列包含 2.4.0,则在执行命令后将其输出显示为 ------。

For eg - If a column contains "2.4.0", after executing the command it gives the output as "------".

推荐答案

您应该为此使用一个类。点表示正则表达式中的任何字符:

You should use a class for that. Dot means any character in regex:

val df3 = df2.withColumn("Current Ver", regexp_replace(col("Current Ver"), "[.]" ,"-"))
df3.show()

另一种解决方案是用反斜杠转义点。

Another solution would be to escape dot with a backslash.

这篇关于正则表达式替换so spark dataframe字符串列中的点字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆