re_exec_all: Extract Data From All Regular Expression Matches Into a Data...

Description Usage Arguments Value Tidy Data Extracting Match Data See Also Examples

View source: R/exec-all.R

Description

Match a regular expression to a string, and return matches, match positions, and capture groups. This function is like its match counterpart, except it returns match/capture group start and end positions in addition to the matched values.

Usage

1

Arguments

text

Character vector.

pattern

A regular expression. See regex for more about regular expressions.

perl

logical should perl compatible regular expressions be used? Defaults to TRUE, setting to FALSE will disable capture groups.

...

Additional arguments to pass to gregexpr (or regexpr if text is of length zero).

Value

A tidy data frame (see Section “Tidy Data”). The entries within the match records within the list columns will be one vectors as long as there are matches for the corresponding text element.

Tidy Data

The return value is a tidy data frame where each row corresponds to an element of the input character vector text. The values from text appear for reference in the .text character column. All other columns are list columns containing the match data. The .match column contains the match information for full regular expression matches while other columns correspond to capture groups if there are any, and PCRE matches are enabled with perl = TRUE (this is on by default). If capture groups are named the corresponding columns will bear those names.

Each match data column list contains match records, one for each element in text. A match record is a named list, with entries match, start and end that are respectively the matching (sub) string, the start, and the end positions (using one based indexing).

Extracting Match Data

To make it easier to extract matching substrings or positions, a special $ operator is defined on match columns, both for the .match column and the columns corresponding to the capture groups. See examples below.

See Also

gregexpr, which this function wraps

Other tidy regular expression matching: re_exec(), re_match_all(), re_match()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
name_rex <- paste0(
  "(?<first>[[:upper:]][[:lower:]]+) ",
  "(?<last>[[:upper:]][[:lower:]]+)"
)
notables <- c(
  "  Ben Franklin and Jefferson Davis",
  "\tMillard Fillmore"
)
# All occurrences
allpos <- re_exec_all(notables, name_rex)
allpos

# Custom $ to extract matches and positions
allpos$first$match
allpos$first$start
allpos$first$end

Example output

# A tibble: 2 x 4
  first      last       .text                                .match    
  <list>     <list>     <chr>                                <list>    
1 <list [3]> <list [3]> "  Ben Franklin and Jefferson Davis" <list [3]>
2 <list [3]> <list [3]> "\tMillard Fillmore"                 <list [3]>
[[1]]
[1] "Ben"       "Jefferson"

[[2]]
[1] "Millard"

[[1]]
[1]  3 20

[[2]]
[1] 2

[[1]]
[1]  5 28

[[2]]
[1] 8

rematch2 documentation built on May 1, 2020, 9:06 a.m.