Do you expect a string to match this pattern?
Usage
expect_match(
object,
regexp,
perl = FALSE,
fixed = FALSE,
...,
all = TRUE,
info = NULL,
label = NULL
)
expect_no_match(
object,
regexp,
perl = FALSE,
fixed = FALSE,
...,
all = TRUE,
info = NULL,
label = NULL
)Arguments
- object
Object to test.
Supports limited unquoting to make it easier to generate readable failures within a function or for loop. See quasi_label for more details.
- regexp
Regular expression to test against.
- perl
logical. Should Perl-compatible regexps be used?
- fixed
If
TRUE, treatsregexpas a string to be matched exactly (not a regular expressions). Overridesperl.- ...
Arguments passed on to
base::greplignore.caselogical. if
FALSE, the pattern matching is case sensitive and ifTRUE, case is ignored during matching.valuelogical. If
FALSE, a vector containing the (integer) indices of the matches determined bygrepis returned, and ifTRUE, a vector containing the matching elements themselves is returned.useByteslogical. If
TRUEthe matching is done byte-by-byte rather than character-by-character. See ‘Details’.invertlogical. If
TRUEreturn indices or values for elements that do not match.replacementa replacement for the matched pattern in
subandgsub. Coerced to character if possible. Forfixed = FALSEthis can include backreferences"\1"to"\9"to parenthesized subexpressions ofpattern. Forperl = TRUEonly, it can also contain"\U"or"\L"to convert the rest of the replacement to upper or lower case and"\E"to end case conversion. If a character vector of length 2 or more is supplied, the first element is used with a warning. IfNA, all elements in the result corresponding to matches will be set toNA.
- all
Should all elements of actual value match
regexp(TRUE), or does only one need to match (FALSE).- info
Extra information to be included in the message. This argument is soft-deprecated and should not be used in new code. Instead see alternatives in quasi_label.
- label
Used to customise failure messages. For expert use only.
Details
expect_match() checks if a character vector matches a regular expression,
powered by grepl().
expect_no_match() provides the complementary case, checking that a
character vector does not match a regular expression.
Examples
expect_match("Testing is fun", "fun")
expect_match("Testing is fun", "f.n")
expect_no_match("Testing is fun", "horrible")
show_failure(expect_match("Testing is fun", "horrible"))
#> Failed expectation:
#> Expected "Testing is fun" to match regexp "horrible".
#> Actual text:
#> ✖ │ Testing is fun
show_failure(expect_match("Testing is fun", "horrible", fixed = TRUE))
#> Failed expectation:
#> Expected "Testing is fun" to match string "horrible".
#> Actual text:
#> ✖ │ Testing is fun
# Zero-length inputs always fail
show_failure(expect_match(character(), "."))
#> Failed expectation:
#> Expected `character()` to have at least one element.
