Skip to content

Extract the target capture group from provided patterns

Source code

Description

Extract the target capture group from provided patterns

Usage

<Expr>$str$extract(pattern, group_index)

Arguments

pattern A valid regex pattern. Can be an Expr or something coercible to an Expr. Strings are parsed as column names.
group_index Index of the targeted capture group. Group 0 means the whole pattern, first group begin at index 1 (default).

Value

String array. Contains null if original value is null or regex capture nothing.

Examples

library("polars")

df = pl$DataFrame(
  a = c(
    "http://vote.com/ballon_dor?candidate=messi&ref=polars",
    "http://vote.com/ballon_dor?candidat=jorginho&ref=polars",
    "http://vote.com/ballon_dor?candidate=ronaldo&ref=polars"
  )
)
df$with_columns(
  extracted = pl$col("a")$str$extract(pl$lit(r"(candidate=(\w+))"), 1)
)
#> shape: (3, 2)
#> ┌─────────────────────────────────┬───────────┐
#> │ a                               ┆ extracted │
#> │ ---                             ┆ ---       │
#> │ str                             ┆ str       │
#> ╞═════════════════════════════════╪═══════════╡
#> │ http://vote.com/ballon_dor?can… ┆ messi     │
#> │ http://vote.com/ballon_dor?can… ┆ null      │
#> │ http://vote.com/ballon_dor?can… ┆ ronaldo   │
#> └─────────────────────────────────┴───────────┘