subExpressionMatches: Find and Extract Regular Expressions from Strings

View source: R/string.R

subExpressionMatchesR Documentation

Find and Extract Regular Expressions from Strings

Description

Find and Extract Regular Expressions from Strings

Usage

subExpressionMatches(
  pattern,
  text,
  match.names = NULL,
  select = stats::setNames(seq_along(match.names), match.names),
  simplify = TRUE,
  regularExpression = NULL
)

Arguments

pattern

regular expression containing parts in parentheses that are to be extracted from text

text

text to be matched against the regular expression

match.names

optional. Names that are to be given to the extracted parts in the result list.

select

named vector of numbers specifying the subexpressions in parentheses to be extracted.

simplify

if TRUE (default) and text has only one element, the output structure will be a list instead a list of lists

regularExpression

deprecated. Use new argument pattern instead

Value

If length(text) > 1 a list is returned with as many elements as there are strings in text each of which is itself a list containing the strings matching the subpatterns (enclosed in parentheses in pattern) or NULL for strings that did not match. If match.names are given, the elements of these lists are named according to the names given in match.names. If text is of length 1 and simplify = TRUE (default) the top level list structure described above is omitted, i.e. the list of substrings matching the subpatterns is returned.

Examples

# split date into year, month and day
subExpressionMatches("(\\\\d{4})\\\\-(\\\\d{2})\\\\-(\\\\d{2})", "2014-04-23")

# split date into year, month and day (give names to the resulting elements)
x <- subExpressionMatches(
  pattern = "(\\\\d{4})\\\\-(\\\\d{2})\\\\-(\\\\d{2})", "2014-04-23",
  match.names = c("year", "month", "day")
)

cat(paste0("Today is ", x$day, "/", x$month, " of ", x$year, "\n"))


KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.