---
title: regexp_substr
description: "regexp\\_substr function in PySpark: Returns the first substring that matches the Java regex regexp within the string str. If the regular expression is not found, the result is null."
canonical_url: https://docs.databricks.com/aws/en/pyspark/reference/functions/regexp_substr
---

# regexp\_substr

Returns the first substring that matches the Java regex `regexp` within the string `str`. If the regular expression is not found, the result is null.

For the corresponding Databricks SQL function, see [`regexp_substr` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_substr.md).

## Syntax

```python
from pyspark.sql import functions as dbf

dbf.regexp_substr(str=<str>, regexp=<regexp>)
```

## Parameters

| Parameter | Type                          | Description               |
| --------- | ----------------------------- | ------------------------- |
| `str`     | `pyspark.sql.Column` or `str` | target column to work on. |
| `regexp`  | `pyspark.sql.Column` or `str` | regex pattern to apply.   |

## Examples

```python
from pyspark.sql import functions as dbf
df = spark.createDataFrame([("1a 2b 14m", r"\d+")], ["str", "regexp"])
```

```python
df.select('*', dbf.regexp_substr('str', dbf.lit(r'\d+'))).show()
df.select('*', dbf.regexp_substr('str', dbf.lit(r'mmm'))).show()
df.select('*', dbf.regexp_substr("str", dbf.col("regexp"))).show()
df.select('*', dbf.regexp_substr(dbf.col("str"), "regexp")).show()
```
