Is Expression Valid and Does It Extract/Remove/Split as Expected?

Usage

test_extract(regex, input, output)
test_remove(regex, input, output)
test_split(regex, input, output)
test_valid(regex)

Arguments

regex
A regular expression to test.
input
The intput string(s) to extract/remove/split from.
output
The desired output of:
extract
regmatches(input, gregexpr(regex, input, perl = TRUE))
remove
gsub(regex, "", input, perl = TRUE)
split
strsplit(input, regex, perl = TRUE)

Is Expression Valid and Does It Extract/Remove/Split as Expected?

Is Expression Valid and Does It Extract/Remove/Split as Expected?

Value

Returns the results of all.equal for the input and desired output.

Description

A logical test of a regular expression's validity, extraction, removal, splitting results. These functions are useful for unit testing of regular expressions.

Details

These functions are inspired by Hadely Wickham's testthat package. They can be used with testthat to test that regular expressions are valid, extracting, removing, and splitting as expected. The user may create their own tests and utilize all.equal or expect_equal to ensure the expression is acting as desired.

Examples

test_extract("\\w+", "I like candy.", list(c("I", "like", "candy")))
[1] TRUE
test_remove("^\\s+|\\s+$", " I like candy ", "I like candy")
[1] TRUE
test_split("(?<=[.?!])\\s+", "I see! When? Oh that's good.", list(c("I see!", "When?", "Oh that's good.")))
[1] TRUE
test_valid("\\w+")
[1] TRUE