Alternation

Share:

Description

Match one string or another.

Usage

1
2
3
4
5
or(..., capture = FALSE)

x %|% y

or1(x, capture = FALSE)

Arguments

...

Character vectors.

capture

A logical value indicating whether or not the result should be captured. See note.

x

A character vector.

y

A character vector.

Value

A character vector representing part or all of a regular expression.

Note

or takes multiple character vector inputs and returns a character vector of the inputs separated by pipes. %|% is an operator interface to this function. or1 takes a single character vector and returns a string collapsed by pipes.

When capture is TRUE, the values are wrapped in a capture group (see capture). When capture is FALSE (the default for or and or1), the values are wrapped in a non-capture group (see token). When capture is NA, (the case for %|%) the values are not wrapped in anything.

References

http://www.regular-expressions.info/alternation.html

See Also

paste

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# or takes an arbitrary number of arguments and groups them without capture
or(letters, LETTERS, "foo")

# or1 takes a single character vector
or1(c(letters, LETTERS, "foo")) # Not the same as before!

# Capture the group
or1(letters, capture = TRUE)

# Don't create a group
or1(letters, capture = NA)

# The pipe operator doesn't group
letters %|% LETTERS %|% "foo"

# Usage
(rx <- or("dog", "cat", "hippopotamus"))
stringi::stri_detect_regex(c("boondoggle", "caterwaul", "water-horse"), rx)