capture: Capture a token, or not

Description Usage Arguments Value References See Also Examples

Description

Create a token to capture or not.

Usage

1
2
3
4
5
6
7

Arguments

x

A character vector.

capture

Logical If TRUE, call capture; if FALSE, call group.

Value

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

References

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

See Also

or for more examples

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
x <- "foo"
capture(x)
group(x)

# Usage
# capture is good with match functions
(rx_price <- capture(digit(1, Inf) %R% DOT %R% digit(2)))
(rx_quantity <- capture(digit(1, Inf)))
(rx_all <- DOLLAR %R% rx_price %R% " for " %R% rx_quantity)
stringi::stri_match_first_regex("The price was $123.99 for 12.", rx_all)

# group is mostly used with alternation.  See ?or.
(rx_spread <- group("peanut butter" %|% "jam" %|% "marmalade"))
stringi::stri_extract_all_regex(
  "You can have peanut butter, jam, or marmalade on your toast.",
  rx_spread
)

Example output

<regex> (foo)
<regex> (?:foo)
<regex> ([[:digit:]]+\.[[:digit:]]{2})
<regex> ([[:digit:]]+)
<regex> \$([[:digit:]]+\.[[:digit:]]{2}) for ([[:digit:]]+)
     [,1]             [,2]     [,3]
[1,] "$123.99 for 12" "123.99" "12"
<regex> (?:peanut butter|jam|marmalade)
[[1]]
[1] "peanut butter" "jam"           "marmalade"    

rebus.base documentation built on May 2, 2019, 5:14 a.m.