pyspark.sql.functions.regexp_extract#
- pyspark.sql.functions.regexp_extract(str, pattern, idx)[source]#
Extract a specific group matched by the Java regex regexp, from the specified string column. If the regex did not match, or the specified group did not match, an empty string is returned.
Added in version 1.5.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters:
- str
Columnor column name target column to work on. A column that evaluates to a string.
- patternstr
regex pattern to apply. A column that evaluates to a string.
- idxint
matched group id. A column that evaluates to an integer.
- str
- Returns:
Columnmatched value specified by idx group id. Returns a column that evaluates to a string.
Examples
>>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame([('100-200',)], ['str']) >>> df.select('*', sf.regexp_extract('str', r'(\d+)-(\d+)', 1)).show() +-------+-----------------------------------+ | str|regexp_extract(str, (\d+)-(\d+), 1)| +-------+-----------------------------------+ |100-200| 100| +-------+-----------------------------------+
>>> df = spark.createDataFrame([('foo',)], ['str']) >>> df.select('*', sf.regexp_extract('str', r'(\d+)', 1)).show() +---+-----------------------------+ |str|regexp_extract(str, (\d+), 1)| +---+-----------------------------+ |foo| | +---+-----------------------------+
>>> df = spark.createDataFrame([('aaaac',)], ['str']) >>> df.select('*', sf.regexp_extract(sf.col('str'), '(a+)(b)?(c)', 2)).show() +-----+-----------------------------------+ | str|regexp_extract(str, (a+)(b)?(c), 2)| +-----+-----------------------------------+ |aaaac| | +-----+-----------------------------------+