Regex replace doesnt match when there is a dot in the text in Scala

Solution for Regex replace doesnt match when there is a dot in the text in Scala
is Given Below:

[*]

I want to capitalize certain words in my spark dataframe. I have the current code which works perfectly for every abbreviation except when there are dots in the words.

val df = List(("Works",1),("D.n.twork",1)).toDF("name","id")

("Works","D.n.twork").foldLeft(df) {
  case (adf, word) =>
    adf.withColumn("name", regexp_replace($"name", "(\b" + word + "\b)", word.toUpperCase))
}

Is there anything I am doing wrong here? It is perfectly fine working with “Works” but “D.n.twork” does not work.

[*]

You can use when and otherwise functions of spark to achieve the required result:

  val spark = SparkSession.builder().master("local[*]").getOrCreate()
  import spark.implicits._
  import org.apache.spark.sql.functions._
  spark.sparkContext.setLogLevel("ERROR")

  // Sample dataframe
  val df = List(("Works",1),("D.n.twork",1), ("sample", 2)).toDF("name","id")

  val wordsToUpper = List("Works","D.n.twork")

  df.withColumn("name", when('name.isin(wordsToUpper:_*), upper('name))
    .otherwise('name)).show(false)

+---------+---+
|name     |id |
+---------+---+
|WORKS    |1  |
|D.N.TWORK|1  |
|sample   |2  |
+---------+---+