PySpark SQL APIs provides regexp_replace
built-in function to replace string values that match with the specified regular expression.
It takes three parameters: the input column of the DataFrame, regular expression and the replacement for matches.
pyspark.sql.functions.regexp_replace(str, pattern, replacement)
Output
The following is the output from this code snippet:
+--------------+-------+----------------+ | str_col|int_col|str_col_replaced| +--------------+-------+----------------+ |Hello Kontext!| 100| Hello kontext!| |Hello Context!| 100| Hello kontext!| +--------------+-------+----------------+
All uppercase 'K' or 'C' are replaced with lowercase 'k'.