Skip to content

Return the index position of the first substring matching a pattern

Source code

Description

Return the index position of the first substring matching a pattern

Usage

<Expr>$str$find(pattern, ..., literal = FALSE, strict = TRUE)

Arguments

pattern A character or something can be coerced to a string Expr of a valid regex pattern, compatible with the regex crate.
Ignored.
literal Logical. If TRUE (default), treat pattern as a literal string, not as a regular expression.
strict Logical. If TRUE (default), raise an error if the underlying pattern is not a valid regex, otherwise mask out with a null value.

Details

To modify regular expression behaviour (such as case-sensitivity) with flags, use the inline (?iLmsuxU) syntax. See the regex crate’s section on grouping and flags for additional information about the use of inline expression modifiers.

Value

An Expr of data type UInt32

See Also

  • $str$start_with(): Check if string values start with a substring.
  • $str$ends_with(): Check if string values end with a substring.
  • $str$contains(): Check if string contains a substring that matches a pattern.

Examples

library(polars)

pl$DataFrame(s = c("AAA", "aAa", "aaa"))$with_columns(
  default_match = pl$col("s")$str$find("Aa"),
  insensitive_match = pl$col("s")$str$find("(?i)Aa")
)
#> shape: (3, 3)
#> ┌─────┬───────────────┬───────────────────┐
#> │ s   ┆ default_match ┆ insensitive_match │
#> │ --- ┆ ---           ┆ ---               │
#> │ str ┆ u32           ┆ u32               │
#> ╞═════╪═══════════════╪═══════════════════╡
#> │ AAA ┆ null          ┆ 0                 │
#> │ aAa ┆ 1             ┆ 0                 │
#> │ aaa ┆ null          ┆ 0                 │
#> └─────┴───────────────┴───────────────────┘