-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathlookarounds.R
58 lines (55 loc) · 1.7 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
50
51
52
53
54
55
56
57
58
#' Negative lookaround functions
#'
#' @description This function facilitates matching by providing negative assurances for surrounding symbols/groups of symbols.
#' It allows for building expressions that are dependent on context of occurrence.
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#' @param value Exact expression to match
#'
#' @examples
#' # matches any number of digits, but not preceded by "USD"
#' rx() %>%
#' rx_avoid_prefix('USD') %>%
#' rx_digit() %>%
#' rx_one_or_more()
#'
#' #matches a digit, but not followed by " dollars"
#' rx() %>%
#' rx_digit() %>%
#' rx_avoid_suffix(' dollars')
#'
#' @rdname rx_avoid
#' @export
rx_avoid_prefix <- function(.data = NULL, value) {
new_rx(paste0(.data, "(?<!", sanitize(value), ")"))
}
#' @rdname rx_avoid
#' @export
rx_avoid_suffix <- function(.data = NULL, value) {
new_rx(paste0(.data, "(?!", sanitize(value), ")"))
}
#' Positive lookaround functions
#'
#' @description This function facilitates matching by providing assurances for surrounding symbols/groups of symbols.
#' It allows for building expressions that are dependent on context of occurrence.
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#' @param value Exact expression to match
#'
#' @examples
#' # this will match anything between square brackets
#' rx() %>%
#' rx_seek_prefix("[") %>%
#' rx_anything("lazy") %>%
#' rx_seek_suffix(']')
#'
#' @rdname rx_seek
#' @export
rx_seek_prefix <- function(.data = NULL, value) {
new_rx(paste0(.data, "(?<=", sanitize(value), ")"))
}
#' @rdname rx_seek
#' @export
rx_seek_suffix <- function(.data = NULL, value) {
new_rx(paste0(.data, "(?=", sanitize(value), ")"))
}