pyspark.sql.functions.rlike#

pyspark.sql.functions.rlike(str, regexp)[source]#

Returns true if str matches the Java regex regexp, or false otherwise.

Added in version 3.5.0.

Parameters:
strColumn or column name

target column to work on. A column that evaluates to a string.

regexpColumn or column name

regex pattern to apply. A column that evaluates to a string.

Returns:
Column

true if str matches a Java regex, or false otherwise. Returns a column that evaluates to a boolean.

Examples

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([("1a 2b 14m", r"(\d+)")], ["str", "regexp"])
>>> df.select('*', sf.rlike('str', sf.lit(r'(\d+)'))).show()
+---------+------+-----------------+
|      str|regexp|RLIKE(str, (\d+))|
+---------+------+-----------------+
|1a 2b 14m| (\d+)|             true|
+---------+------+-----------------+
>>> df.select('*', sf.rlike('str', sf.lit(r'\d{2}b'))).show()
+---------+------+------------------+
|      str|regexp|RLIKE(str, \d{2}b)|
+---------+------+------------------+
|1a 2b 14m| (\d+)|             false|
+---------+------+------------------+
>>> df.select('*', sf.rlike("str", sf.col("regexp"))).show()
+---------+------+------------------+
|      str|regexp|RLIKE(str, regexp)|
+---------+------+------------------+
|1a 2b 14m| (\d+)|              true|
+---------+------+------------------+
>>> df.select('*', sf.rlike("str", "regexp")).show()
+---------+------+------------------+
|      str|regexp|RLIKE(str, regexp)|
+---------+------+------------------+
|1a 2b 14m| (\d+)|              true|
+---------+------+------------------+