regexp {utilitiesR}R Documentation

Extract captured groups in a (Perl) regex.

Description

Extract captured groups in a (Perl) regex.

Usage

  regexp(pattern, text, ...)

Arguments

pattern

the regex (in Perl), with capturing brackets, possibly named.

text

the string or character string to test the regex against

...

passed into regexpr or gregexpr (e.g. ignore.case)

Details

If there are no capturing groups in the pattern, an error is thrown. If there are capturing groups but a group simply does not have a match in the string, that entry is the empty string "".

Value

A matrix with the same number of rows as 'text' and one column per captured group. If named captured groups are used (e.g. '(?<num>[0-9]+)'), the names will be the column names.

Note

This is essentially stringr's str_matches but avoids me loading stringr in.

See Also

gregexp for the same thing, but using the 'g' flag (yielding much more unwieldy output)

Examples

regexp('^(?<greeting>.+?)[, ]+(?<target>.+)$', c('Hello, world', 'Hi bar'))
#      greeting target
# [1,] "Hello"  "world"
# [2,] "Hi"     "bar"

[Package utilitiesR version 2.0 Index]