我如何替换所有以字符串开头的特殊字符

时间:2019-02-11 16:32:47

标签: apache-spark-sql

输入:

rstan::stan_model

预期输出:

+---------+
|full_name|
+---------+
|    $raja|
|    #raja|
|    .raja|

我的代码仅从第一条记录中删除了+---------+ |full_name| +---------+ | raja| | raja| | raja| val df2 = df1.select("full_name").withColumn("full_name",regexp_replace(col("full_name"), "^[$_]", "")) 。如何获得预期的输出?

1 个答案:

答案 0 :(得分:0)

这是因为在正则表达式中,您仅搜索字符串开头的$符号。尝试添加要替换的其他符号,如下所示:

val df2 = df1.withColumn("full_name",regexp_replace(col("full_name"), "^[$#\\._]",  ""))

获得预期的输出:

+---------+
|full_name|
+---------+
|     raja|
|     raja|
|     raja|
+---------+