match_pattern {chinese.misc}R Documentation

Extract Strings by Regular Expression Quickly

Description

Given a pattern and a character vector, the function will extract parts of these characters that match the pattern. It is simply a wrapper of regmatches.

Usage

match_pattern(pattern, where, vec_result = TRUE, perl = FALSE)

Arguments

pattern

a length 1 regular expression to be matched.

where

a character vector, each of its elements may or may not have parts that match the specified pattern.

vec_result

should be TRUE or FALSE. If TRUE (default), all matched parts will be returned in a character vector. If FALSE, a list is returned, each element of the list represents the matching result of the corresponding element in where. If an element in where has nothing matching the pattern, the result is still an element in the list and assigned character(0).

perl

default is FALSE. Should Perl-compatible regexps be used?

Value

a character vector or a list. If an element in where is NA, the result corresponds to this element is character(0).

Examples

p <- "x.*?y"
x <- c("x6yx8y", "x10yx30y", "aaaaaa", NA, "x00y")
y <- match_pattern(p, x)
y <- match_pattern(p, x, vec_result = FALSE)

[Package chinese.misc version 0.2.3 Index]