grepex: Extract portions of string(s) with regular expressions

View source: R/grepex.R

grepexR Documentation

Extract portions of string(s) with regular expressions

Description

Given a character vector extract portions of each element with regular expressions.

Usage

grepex(pattern, x, multiple = FALSE, ...)

Arguments

pattern

character vector, regular expressions

x

character vector, strings from which to extract

multiple

logical, currently ignored

...

other arguments passed to regexpr

Details

Every element of x is searched with patterns in pattern using regexpr. The matches are then extracted.

Value

A matrix r, say, with length(pattern) columns and length(x) rows. The entry r[i,j] of this matrix is a portion of x[i] which is first match of pattern[i].

See Also

regexpr

Examples


x <- c("On 2010-02-01 John Smith had a beer",
        "On 2010-02-02 Adam Smith did not have a beer",
        "On 2010-02-03 Bob Smith had a beer again")

re <- c( isodate="\\<[0-9]{4}-[0-9]{2}-[0-9]{2}\\>",
        name="\\<[A-Z][a-z]+\\> \\<[A-Z][a-z]+\\>",
        beer="\\<beer\\>")

grepex(re, x)


mbojan/mbtools documentation built on Oct. 16, 2023, 8:18 p.m.