regextract: Extract (sub)strings matching regex pattern

Description Usage Arguments Details Value Author(s) Examples

Description

Extract (sub)strings matching regex pattern.

Usage

1

Arguments

x

a character vector.

pattern

regular expression to be found in x.

...

not used.

Details

Extract the substrings of x that match the regex pattern pattern. The pattern may contain groups (enclosed in parentheses) which will result in further substrings extracted.

Derived from the help on regmatches, take a look at the help there

Value

a character (string) matrix where the first column contains the global match for the pattern, each pair of '()' will result in another column with the respective match.

Author(s)

Benno Pütz puetz@psych.mpg.de

Examples

1
2
3
4
5
6
s <- "Test: A1 BC23 DEF456"
pattern = "([[:alpha:]]+)([[:digit:]]+)"
regextract(s, pattern)

# equivalent to this example from the help page for grep()
lapply(regmatches(s, gregexpr(pattern, s)), function(e) regmatches(e, regexec(pattern, e)))

Example output

     [,1] [,2] [,3] [,4]   [,5] [,6] [,7]     [,8]  [,9] 
[1,] "A1" "A"  "1"  "BC23" "BC" "23" "DEF456" "DEF" "456"
[[1]]
[[1]][[1]]
[1] "A1" "A"  "1" 

[[1]][[2]]
[1] "BC23" "BC"   "23"  

[[1]][[3]]
[1] "DEF456" "DEF"    "456"   

SimPhe documentation built on May 1, 2019, 9:10 p.m.