Skip to contents

re_split splits each element of a character vector into substrings based on a specified pattern (regular expression). If the provided pattern is not already a compiled pattern object, it compiles it using re_compile. The function allows for controlling the maximum number of splits performed.

Usage

re_split(pattern, string, ..., maxsplit = -1L)

Arguments

pattern

A regular expression pattern or a compiled pattern object.

string

A character vector where each element is a string to be split.

...

Arguments passed on to re_compile

IGNORECASE

Flag to indicate case-insensitive matching.

I

Abbreviation for IGNORECASE.

MULTILINE

Flag to indicate multi-line matching, where ^ and $ match the start and end of each line.

M

Abbreviation for MULTILINE.

DOTALL

Flag to indicate that . (dot) should match any character including newline.

S

Abbreviation for DOTALL

VERBOSE

Flag to allow a more verbose regex syntax, which can include comments and whitespace for readability.

X

Abbreviation for VERBOSE

NOFLAG

Flag to indicate that no flags should be set.

maxsplit

The maximum number of splits to perform on each string. If -1L (default), all possible splits are performed.

Value

A list of character vectors, where each vector contains the substrings resulting from splitting the corresponding element of string.

Examples

pattern <- re_compile("\\s+")
re_split(pattern, "Split this string") # Splits on whitespace
#> [[1]]
#> [1] "Split"  "this"   "string"
#> 
re_split("\\W+", "Split,with!punctuation.morestuff", maxsplit = 2)
#> [[1]]
#> [1] "Split"                 "with"                  "punctuation.morestuff"
#>