Lookaround

Share:

Description

Zero length matching.

Usage

1
2
3
4
5
6
7

Arguments

x

A character vector.

Value

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

Note

Lookbehind is not supported by R's PRCE engine. Use R's Perl engine or stringi/stringr's ICU engine.

References

http://www.regular-expressions.info/lookaround.html and http://www.rexegg.com/regex-lookarounds.html

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
x <- "foo"
lookahead(x)
negative_lookahead(x)
lookbehind(x)
negative_lookbehind(x)

# Usage
x <- c("mozambique", "qatar", "iraq")
(rx_neg_class <- "q" %R% negated_char_class("u"))
(rx_neg_lookahead <- "q" %R% negative_lookahead("u"))
stringi::stri_detect_regex(x, rx_neg_class)
stringi::stri_detect_regex(x, rx_neg_lookahead)

# PRCE engine doesn't support lookbehind
x2 <- c("queen", "vacuum")
(rx_lookbehind <- lookbehind("q")) %R% "u"
stringi::stri_detect_regex(x2, rx_lookbehind)
try(grepl(rx_lookbehind, x2))
try(grepl(rx_lookbehind, x2, perl = TRUE))