Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion python/pyspark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2754,11 +2754,22 @@ def split(str, pattern, limit=-1):


def regexp_extract(str, pattern, idx):
r"""Extract a specific group matched by a Java regex, from the specified string column.
r"""Extract the first match of the given group in a regex, from the specified string column.
If the regex did not match, or the specified group did not match, an empty string is returned.

.. versionadded:: 1.5.0

Parameters
----------
str : :class:`Column` or str
a string expression to extract from
pattern : str
a string representing a regular expression. The regex string should be a Java regular
expression.
idx : int
the regex group from which to extract a potential match. 0 stands for the entire regex, and
only non-negative numbers are allowed.

Examples
--------
>>> df = spark.createDataFrame([('100-200',)], ['str'])
Expand All @@ -2776,6 +2787,40 @@ def regexp_extract(str, pattern, idx):
return Column(jc)


def regexp_extract_all(str, pattern, idx):
r"""Extract all matches of the given group in a regex, from the specified string column.
If the regex did not match, or the specified group did not match, an empty array is returned.

.. versionadded:: 3.1.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's target 3.2.0

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I thought this was the version where the Scala function was introduced. Changed.


Parameters
----------
str : :class:`Column` or str
a string expression to extract from
pattern : str
a string representing a regular expression. The regex string should be a Java regular
expression.
idx : int
the regex group from which to extract all matches. 0 stands for the entire regex, and only
non-negative numbers are allowed.

Examples
--------
>>> df = spark.createDataFrame([('100-200, 300-400',)], ['str'])
>>> df.select(regexp_extract_all('str', r'(\d+)-(\d+)', 1).alias('d')).collect()
[Row(d=['100', '300'])]
>>> df = spark.createDataFrame([('foo',)], ['str'])
>>> df.select(regexp_extract_all('str', r'(\d+)', 1).alias('d')).collect()
[Row(d=[])]
>>> df = spark.createDataFrame([('aaaac',)], ['str'])
>>> df.select(regexp_extract_all('str', '(a+)(b)?(c)', 2).alias('d')).collect()
[Row(d=[''])]
"""
sc = SparkContext._active_spark_context
jc = sc._jvm.functions.regexp_extract_all(_to_java_column(str), pattern, idx)
return Column(jc)


def regexp_replace(str, pattern, replacement):
r"""Replace all substrings of the specified string value that match regexp with rep.

Expand Down