-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathlookarounds.R
49 lines (44 loc) · 1.33 KB
/
lookarounds.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#' @include escape.R
#' @include utils.R
NULL
#' Lookarounds
#'
#' These functions provide an interface to perl lookarounds.
#'
#' Special binary functions are used to infer an ordering, since often you
#' might wish to match a word / set of characters conditional on the start
#' and end of that word.
#'
#' \itemize{
#' \item \code{\%if_next_is\%}: \code{TRUE} if x follows y
#' \item \code{\%if_next_isnt\%}: \code{TRUE} if x does not follow y
#' \item \code{\%if_prev_is\%}: \code{TRUE} if y comes before x
#' \item \code{\%if_prev_isnt\%}: \code{TRUE} if y does not come before x
#' }
#' @param x A regex pattern.
#' @param y A regex pattern.
#' @name lookarounds
#' @title Lookarounds
#' @family rex
#' @seealso Perl 5 Documentation \url{http://perldoc.perl.org/perlre.html#Extended-Patterns}
NULL
#' @rdname lookarounds
`%if_next_is%` <- function(x, y) {
p("(?:", escape(x), "(?=", escape(y), ")", ")")
}
register(`%if_next_is%`)
#' @rdname lookarounds
`%if_next_isnt%` <- function(x, y) {
p("(?:", escape(x), "(?!", escape(y), ")", ")")
}
register(`%if_next_isnt%`)
#' @rdname lookarounds
`%if_prev_is%` <- function(x, y) {
p("(?:", "(?<=", escape(y), ")", escape(x), ")")
}
register(`%if_prev_is%`)
#' @rdname lookarounds
`%if_prev_isnt%` <- function(x, y) {
p("(?:", "(?<!", escape(y), ")", escape(x), ")")
}
register(`%if_prev_isnt%`)