Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding lazy mode, lookarounds, none_or_more, count and digits #7

Merged
merged 3 commits into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ export(rx)
export(rx_any_of)
export(rx_anything)
export(rx_anything_but)
export(rx_avoid_prefix)
export(rx_avoid_suffix)
export(rx_begin_capture)
export(rx_br)
export(rx_count)
export(rx_digit)
export(rx_digits)
export(rx_end_capture)
export(rx_end_of_line)
export(rx_find)
export(rx_line_break)
export(rx_maybe)
export(rx_none_or_more)
export(rx_not)
export(rx_one_or_more)
export(rx_or)
export(rx_range)
export(rx_seek_prefix)
export(rx_seek_suffix)
export(rx_something)
export(rx_something_but)
export(rx_start_of_line)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
* Deprecated `then()` and `any()` in favor of `any_of()` and `find()`.
* `rx_` prefix added per [Dmytro Perepolkin's](https://github.com/dmi3kno) suggestion [(#1)](https://github.com/VerbalExpressions/RVerbalExpressions/issues/1)
* Add `rx()` constructor (#4)
* Added lazy mode for `rx_anything()`, `rx_anything_but()`, `rx_something()`, `rx_something_but()`.

13 changes: 11 additions & 2 deletions R/anything.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
#' (except line breaks) 0 or more times.
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#' @param mode Matching mode (\code{greedy} (default) or\code{lazy}). \code{Lazy} matching stops after the first match, \code{greedy} continues
#' searching until end of the string and then back-tracks to the last match.
#'
#' @examples
#' rx_anything()
#' rx_anything(mode = "lazy")
#'
#' x <- rx_start_of_line() %>%
#' rx_anything() %>%
Expand All @@ -23,7 +26,13 @@
#' Dot: \url{https://www.regular-expressions.info/dot.html}
#'
#' Star Quantifier: \url{https://www.regular-expressions.info/repeat.html}
#'
#' Greedy and Lazy Quantifiers: \url{https://www.regular-expressions.info/repeat.html#greedy}
#' @export
rx_anything <- function(.data = NULL) {
paste0(.data, "(?:.*)")
rx_anything <- function(.data = NULL, mode = "greedy") {
switch(mode,
greedy = paste0(.data, "(?:.*)"),
lazy = paste0(.data, "(?:.*?)"),
stop("Please, provide valid 'mode' argument")
)
}
10 changes: 8 additions & 2 deletions R/anything_but.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#' @param value Characters to not match
#' @param mode Matching mode (\code{greedy} (default) or\code{lazy}). \code{Lazy} matching stops after the first match, \code{greedy} continues
#' searching until end of the string and then back-tracks to the last match.
#'
#' @examples
#' rx_anything_but(value = "abc")
#'
#' @references
#' Character Class: \url{https://www.regular-expressions.info/charclass.html}
#' @export
rx_anything_but <- function(.data = NULL, value) {
paste0(.data, "(?:[^", sanitize(value), "]*)")
rx_anything_but <- function(.data = NULL, value, mode = "greedy") {
switch(mode,
greedy = paste0(.data, "(?:[^", sanitize(value), "]*)"),
lazy = paste0(.data, "(?:[^", sanitize(value), "]*?)"),
stop("Please, provide valid 'mode' argument")
)
}
26 changes: 26 additions & 0 deletions R/avoid.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' 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 occurence.
#'
#' @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) {
paste0(.data, "(?<!", sanitize(value), ")")
}

#' @rdname rx_avoid
#' @export
rx_avoid_suffix <- function(.data = NULL, value) {
paste0(.data, "(?!", sanitize(value), ")")
}
24 changes: 24 additions & 0 deletions R/count.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' Match the previous stuff exact number of times.
#'
#' @description This function simply adds a \code{{n}} to the end of the expression.
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#' @param n Number of times previous expression shall be repeated. Default is 1.
#'
#' @examples
#' rx_count()
#'
#' # create an expression
#' x <- rx_find(value = "a") %>%
#' rx_count(3)
#'
#' # create input
#' input <- "aaa"
#'
#' # extract match
#' regmatches(input, regexpr(x, input))
#'
#' @export
rx_count <- function(.data = NULL, n = 1) {
paste0(.data, "{", n,"}")
}
14 changes: 11 additions & 3 deletions R/digit.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#' Match a digit (0–9).
#'
#' @details This function is looks for tabs with the following expression:
#' \code{\\d}
#'
#' @details The function \code{rx_digit()}looks for tabs with the following expression:
#' \code{\\d} and matches single digit. Plural version matches specified number of digits \code{n}
#' (equivalent to \code{rx_digit() %>% rx_count(n)}).
#' @rdname rx_digit
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#'
#' @examples
Expand All @@ -20,3 +21,10 @@
rx_digit <- function(.data = NULL) {
paste0(.data, "\\d")
}

#' @param n Exact number of digits to match.
#' @rdname rx_digit
#' @export
rx_digits <- function(.data = NULL, n = 1) {
paste0(.data, "\\d", "{", n, "}")
}
39 changes: 37 additions & 2 deletions R/one_or_more.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#' @description This function simply adds a + to the end of the expression.
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#' @param mode Matching mode (\code{greedy} (default) or\code{lazy}). \code{Lazy} matching stops after the first match, \code{greedy} continues
#' searching until end of the string and then back-tracks to the last match.
#'
#' @examples
#' rx_one_or_more()
Expand All @@ -17,6 +19,39 @@
#' # extract match
#' regmatches(input, regexpr(x, input))
#' @export
rx_one_or_more <- function(.data = NULL) {
paste0(.data, "+")
rx_one_or_more <- function(.data = NULL, mode = "greedy") {
switch(mode,
greedy = paste0(.data, "+"),
lazy = paste0(.data, "+?"),
stop("Please, provide valid 'mode' argument")
)
}

#' Match the previous stuff zero or many times.
#'
#' @description This function simply adds a * to the end of the expression.
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#' @param mode Matching mode (\code{greedy} (default) or\code{lazy}). \code{Lazy} matching stops after the first match, \code{greedy} continues
#' searching until end of the string and then back-tracks to the last match.
#'
#' @examples
#' rx_none_or_more()
#'
#' # create an expression
#' x <- rx_find(value = "a") %>%
#' rx_none_or_more()
#'
#' # create input
#' input <- "aaa"
#'
#' # extract match
#' regmatches(input, regexpr(x, input))
#' @export
rx_none_or_more <- function(.data = NULL, mode = "greedy") {
switch(mode,
greedy = paste0(.data, "*"),
lazy = paste0(.data, "*?"),
stop("Please, provide valid 'mode' argument")
)
}
26 changes: 26 additions & 0 deletions R/seek.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' 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 occurence.
#'
#' @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) {
paste0(.data, "(?<=", sanitize(value), ")")
}

#' @rdname rx_seek
#' @export
rx_seek_suffix <- function(.data = NULL, value) {
paste0(.data, "(?=", sanitize(value), ")")
}
12 changes: 10 additions & 2 deletions R/something.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#' \code{anything()} expects \emph{anything} including... nothing!
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#' @param mode Matching mode (\code{greedy} (default) or\code{lazy}). \code{Lazy} matching stops after the first match, \code{greedy} continues
#' searching until end of the string and then back-tracks to the last match.
#'
#' @examples
#' rx_something()
Expand All @@ -19,7 +21,13 @@
#'
#' @references
#' Metacharacters: \url{https://www.regular-expressions.info/characters.html#special}
#'
#' Greedy and Lazy Quantifiers: \url{https://www.regular-expressions.info/repeat.html#greedy}
#' @export
rx_something <- function(.data = NULL) {
paste0(.data, "(?:.+)")
rx_something <- function(.data = NULL, mode="greedy") {
switch(mode,
greedy = paste0(.data, "(?:.+)"),
lazy = paste0(.data, "(?:.+?)"),
stop("Please, provide valid 'mode' argument")
)
}
12 changes: 10 additions & 2 deletions R/something_but.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#' @param value Expression to optionally match
#' @param mode Matching mode (\code{greedy} (default) or\code{lazy}). \code{Lazy} matching stops after the first match, \code{greedy} continues
#' searching until end of the string and then back-tracks to the last match.
#'
#' @examples
#' rx_something_but(value = "abc")
Expand All @@ -19,7 +21,13 @@
#'
#' @references
#' Metacharacters: \url{https://www.regular-expressions.info/characters.html#special}
#'
#' Greedy and Lazy Quantifiers: \url{https://www.regular-expressions.info/repeat.html#greedy}
#' @export
rx_something_but <- function(.data = NULL, value) {
paste0(.data, "(?:[^", sanitize(value), "]+)")
rx_something_but <- function(.data = NULL, value, mode="greedy") {
switch(mode,
greedy = paste0(.data, "(?:[^", sanitize(value), "]+)"),
lazy = paste0(.data, "(?:[^", sanitize(value), "]+?)"),
stop("Please, provide valid 'mode' argument")
)
}
8 changes: 7 additions & 1 deletion man/rx_anything.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/rx_anything_but.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/rx_avoid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions man/rx_count.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading