Skip to content

Commit

Permalink
add rx_ prefix, closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerlittlefield committed Mar 8, 2019
1 parent 22e441d commit ae2e5ed
Show file tree
Hide file tree
Showing 97 changed files with 5,080 additions and 425 deletions.
8 changes: 3 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
Package: RVerbalExpressions
Title: Create regular expressions easily
Version: 0.0.0.9000
Authors@R:
person(given = "Tyler",
family = "Littlefield",
role = c("aut", "cre"),
email = "[email protected]")
Authors@R: as.person(c(
"Tyler Littlefield <[email protected]> [aut, cre]"
))
Description: The goal of `RVerbalExpressions` is to make it easier to construct
regular expressions. Grammar and functionality inspired by
[VerbalExpressions](https://github.com/VerbalExpressions). Usage of the
Expand Down
45 changes: 23 additions & 22 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(any_of)
export(anything)
export(anything_but)
export(begin_capture)
export(br)
export(digit)
export(end_capture)
export(end_of_line)
export(find)
export(line_break)
export(maybe)
export(not)
export(one_or_more)
export(or)
export(range)
export(rx)
export(rx_any_of)
export(rx_anything)
export(rx_anything_but)
export(rx_begin_capture)
export(rx_br)
export(rx_digit)
export(rx_end_capture)
export(rx_end_of_line)
export(rx_find)
export(rx_line_break)
export(rx_maybe)
export(rx_not)
export(rx_one_or_more)
export(rx_or)
export(rx_range)
export(rx_something)
export(rx_something_but)
export(rx_start_of_line)
export(rx_tab)
export(rx_whitespace)
export(rx_with_any_case)
export(rx_word)
export(sanitize)
export(something)
export(something_but)
export(start_of_line)
export(tab)
export(whitespace)
export(with_any_case)
export(word)
importFrom(magrittr,"%>%")
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RVerbalExpressions 0.0.0.9000

* Added a `NEWS.md` file to track changes to the package.
* Deprecated `then()` and `any` in favor of `any_of()` and `find()`.
* Deprecated `then()` and `any()` in favor of `any_of()` and `find()`.
6 changes: 3 additions & 3 deletions R/any_of.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
#' @param value Expression to optionally match
#'
#' @examples
#' any_of(value = "abc")
#' rx_any_of(value = "abc")
#'
#' # create an expression
#' x <- any_of(value = "abc")
#' x <- rx_any_of(value = "abc")
#'
#' grepl(x, "c") # should be true
#' grepl(x, "d") # should be false
#'
#' @references
#' Character class: \url{https://www.regular-expressions.info/charclass.html}
#' @export
any_of <- function(.data = NULL, value) {
rx_any_of <- function(.data = NULL, value) {
paste0(.data, "[", sanitize(value), "]")
}
14 changes: 7 additions & 7 deletions R/anything.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#'
#' @examples
#' anything()
#' rx_anything()
#'
#' x <- start_of_line() %>%
#' anything() %>%
#' end_of_line()
#' x <- rx_start_of_line() %>%
#' rx_anything() %>%
#' rx_end_of_line()
#'
#' grepl(x, "anything!") # this should be true
#' grepl(anything(), "") # this should be true
#' grepl(something(), "") # this should be false
#' grepl(rx_anything(), "") # this should be true
#' grepl(rx_something(), "") # this should be false
#'
#' @references
#' Dot: \url{https://www.regular-expressions.info/dot.html}
#'
#' Star Quantifier: \url{https://www.regular-expressions.info/repeat.html}
#' @export
anything <- function(.data = NULL) {
rx_anything <- function(.data = NULL) {
paste0(.data, "(?:.*)")
}
4 changes: 2 additions & 2 deletions R/anything_but.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
#' @param value Characters to not match
#'
#' @examples
#' anything_but(value = "abc")
#' rx_anything_but(value = "abc")
#'
#' @references
#' Character Class: \url{https://www.regular-expressions.info/charclass.html}
#' @export
anything_but <- function(.data = NULL, value) {
rx_anything_but <- function(.data = NULL, value) {
paste0(.data, "(?:[^", sanitize(value), "]*)")
}
8 changes: 4 additions & 4 deletions R/br.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#' Match a line break (alias of \code{line_break()}).
#' Match a line break (alias of \code{rx_line_break()}).
#'
#' @description This expression looks for line breaks, both Unix and Windows
#' style by using the appropriate \emph{non printable characters}.
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#'
#' @examples
#' br()
#' rx_br()
#'
#' # create an expression
#' x <- br()
#' x <- rx_br()
#'
#' # create input
#' string <- "foo\nbar"
Expand All @@ -24,6 +24,6 @@
#'
#' Non printable character: \url{https://www.regular-expressions.info/nonprint.html}
#' @export
br <- function(.data = NULL) {
rx_br <- function(.data = NULL) {
paste0(.data, "(?:\\r\\n|\\r|\\n)")
}
4 changes: 2 additions & 2 deletions R/capture_groups.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#'
#' @export
begin_capture <- function(.data = NULL) {
rx_begin_capture <- function(.data = NULL) {
if(is.null(.data)) {
paste0("(")
} else {
Expand All @@ -22,7 +22,7 @@ begin_capture <- function(.data = NULL) {
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#'
#' @export
end_capture <- function(.data = NULL) {
rx_end_capture <- function(.data = NULL) {
if(is.null(.data)) {
paste0(")")
} else {
Expand Down
6 changes: 3 additions & 3 deletions R/digit.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#'
#' @examples
#' digit()
#' rx_digit()
#'
#' # create an expression
#' x <- digit()
#' x <- rx_digit()
#'
#' # create input
#' string <- "1 apple"
#'
#' # extract match
#' regmatches(string, regexpr(x, string))
#' @export
digit <- function(.data = NULL) {
rx_digit <- function(.data = NULL) {
paste0(.data, "\\d")
}
14 changes: 7 additions & 7 deletions R/end_of_line.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
#' @param enable Whether to enable this behavior, defaults to \code{TRUE}
#'
#' @examples
#' end_of_line(enable = TRUE)
#' end_of_line(enable = FALSE)
#' end_of_line("abc", enable = TRUE)
#' rx_end_of_line(enable = TRUE)
#' rx_end_of_line(enable = FALSE)
#' rx_end_of_line("abc", enable = TRUE)
#'
#' # create expression
#' x <- start_of_line(FALSE) %>%
#' find("apple") %>%
#' end_of_line()
#' x <- rx_start_of_line(FALSE) %>%
#' rx_find("apple") %>%
#' rx_end_of_line()
#'
#' grepl(x, "apples") # should be false
#' grepl(x, "apple") # should be true
#'
#' @references
#' Anchors: \url{https://www.regular-expressions.info/anchors.html}
#' @export
end_of_line <- function(.data = NULL, enable = TRUE) {
rx_end_of_line <- function(.data = NULL, enable = TRUE) {
if (enable) {
paste0(.data, "$")
} else {
Expand Down
6 changes: 3 additions & 3 deletions R/find.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#' @param value Exact expression to match
#'
#' @examples
#' find(value = "apple")
#' rx_find(value = "apple")
#'
#' # create expression
#' x <- find(value = "apples")
#' x <- rx_find(value = "apples")
#'
#' grepl(x, "apple") # should be false
#' grepl(x, "apples") # should be true
Expand All @@ -20,6 +20,6 @@
#'
#' Stack Overflow: \url{https://stackoverflow.com/questions/3512471}
#' @export
find <- function(.data = NULL, value) {
rx_find <- function(.data = NULL, value) {
paste0(.data, "(?:", sanitize(value), ")")
}
6 changes: 3 additions & 3 deletions R/line_break.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#'
#' @examples
#' line_break()
#' rx_line_break()
#'
#' # create an expression
#' x <- line_break()
#' x <- rx_line_break()
#'
#' # create input
#' string <- "foo\nbar"
Expand All @@ -24,6 +24,6 @@
#'
#' Non printable character: \url{https://www.regular-expressions.info/nonprint.html}
#' @export
line_break <- function(.data = NULL) {
rx_line_break <- function(.data = NULL) {
paste0(.data, "(?:\\r\\n|\\r|\\n)")
}
10 changes: 5 additions & 5 deletions R/maybe.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
#' @param value Expression to optionally match
#'
#' @examples
#' maybe(value = "abc")
#' rx_maybe(value = "abc")
#'
#' # create expression
#' x <- start_of_line() %>%
#' maybe("abc") %>%
#' end_of_line(enable = FALSE)
#' x <- rx_start_of_line() %>%
#' rx_maybe("abc") %>%
#' rx_end_of_line(enable = FALSE)
#'
#' grepl(x, "xyz") # should be true
#'
#' @references
#' Quantifiers: \url{https://www.regular-expressions.info/optional.html}
#' @export
maybe <- function(.data = NULL, value) {
rx_maybe <- function(.data = NULL, value) {
paste0(.data, "(?:", sanitize(value), ")?")
}
14 changes: 7 additions & 7 deletions R/not.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
#' @param value Value to ensure absence of
#'
#' @examples
#' not(value = "FEB-28")
#' rx_not(value = "FEB-28")
#'
#' # construct expression
#' x <- start_of_line() %>%
#' find('FEB-29') %>%
#' not("FEB-28")
#' x <- rx_start_of_line() %>%
#' rx_find('FEB-29') %>%
#' rx_not("FEB-28")
#'
#' # create a string
#' string <- c("FEB-29-2017", "FEB-28-2017")
Expand All @@ -24,14 +24,14 @@
#' regmatches(string, regexpr(x, string, perl = TRUE))
#'
#' # another example
#' find(value = "q") %>%
#' not("u") %>%
#' rx_find(value = "q") %>%
#' rx_not("u") %>%
#' grepl(x = c("qu", "qa", "qq", "q", "q u"), perl = TRUE)
#'
#' @references
#' Negative lookahead: \url{https://www.regular-expressions.info/lookaround.html}
#'
#' @export
not <- function(.data = NULL, value) {
rx_not <- function(.data = NULL, value) {
paste0(.data, "(?!", sanitize(value), ")")
}
9 changes: 5 additions & 4 deletions R/one_or_more.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#'
#' @examples
#' one_or_more()
#' rx_one_or_more()
#'
#' # create an expression
#' x <- find(value = "a") %>%
#' one_or_more()
#' x <- rx_find(value = "a") %>%
#' rx_one_or_more()
#'
#' # create input
#' input <- "aaa"
#'
#' # extract match
#' regmatches(input, regexpr(x, input))
#' @export
one_or_more <- function(.data = NULL) {
rx_one_or_more <- function(.data = NULL) {
paste0(.data, "+")
}
6 changes: 3 additions & 3 deletions R/or.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#'
#' @examples
#' # create an expression
#' x <- find(value = "foo") %>%
#' or(find(value = "bar"))
#' x <- rx_find(value = "foo") %>%
#' rx_or(rx_find(value = "bar"))
#'
#' # create strings
#' string1 <- "foo!"
Expand All @@ -22,7 +22,7 @@
#' regmatches(string2, gregexpr(x, string2))[[1]]
#'
#' @export
or <- function(.data, value) {
rx_or <- function(.data, value) {
# Not sure if I like this. I would prefer:
#
# find(value = "foo") %>%
Expand Down
6 changes: 3 additions & 3 deletions R/range.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
#' parameters; unpaired parameters are ignored.
#'
#' @examples
#' range(value = c('1', '3'))
#' rx_range(value = c('1', '3'))
#'
#' # create an expression
#' x <- range(value = c('1', '3'))
#' x <- rx_range(value = c('1', '3'))
#'
#' grepl(x, "2") # should be true
#' grepl(x, "4") # should be false
#' @export
range <- function(.data = NULL, value) {
rx_range <- function(.data = NULL, value) {
value <- split(value, ceiling(seq_along(value)/2))
value <- value[lengths(value) == 2]
value <- lapply(value, function(i) paste0(i[1], "-", i[2]))
Expand Down
Loading

0 comments on commit ae2e5ed

Please sign in to comment.