Skip to content

Commit

Permalink
Merge pull request #10 from dmi3kno/master
Browse files Browse the repository at this point in the history
Added `rx_word_char()`, `rx_word_edge()`, extended `rx_count()`
  • Loading branch information
tylerlittlefield authored Mar 10, 2019
2 parents 847c850 + 9bd4102 commit 2cc018a
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 4 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Authors@R: c(
person("Tyler", "Littlefield", email = "[email protected]", role = c("aut", "cre")),
person("Dmytro", "Perepolkin", email = "[email protected]", role = c("ctb"), comment=c(ORCID="0000-0001-8558-6183")))
Description: The goal of `RVerbalExpressions` is to make it easier to construct
regular expressions using rammar and functionality inspired by
regular expressions using grammar and functionality inspired by
[VerbalExpressions](https://github.com/VerbalExpressions). Usage of the
`%>%` is encouraged to build expressions in a chain like fashion.
`%>%` is encouraged to build expressions in a chain-like fashion.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@ export(rx_uppercase)
export(rx_whitespace)
export(rx_with_any_case)
export(rx_word)
export(rx_word_char)
export(rx_word_edge)
export(sanitize)
importFrom(magrittr,"%>%")
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
* Add `rx()` constructor (#4)
* Added lazy mode for `rx_anything()`, `rx_anything_but()`, `rx_something()`, `rx_something_but()`.
* Added `rx_lowercase()`, `rx_uppercase()`, `rx_alnum()`, `rx_alpha()`.
* Added `rx_word_char()`, `rx_word_edge()`, extended `rx_count()` to ranges of repetition

9 changes: 8 additions & 1 deletion R/count.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#' @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.
#' @param n Number of times previous expression shall be repeated.
#' For exact number of repetitions use single number. Use sequence \code{min:max} or vector of \code{c(min, max)} to denote range of repetitions.
#' To create ranges unbounded on one end, pass on a vector with either first of second element equal to \code{NA}, e.g. \code{c(NA, 3)},
#' up to 3 repetitions.
#'
#' @examples
#' rx_count()
Expand All @@ -20,5 +23,9 @@
#'
#' @export
rx_count <- function(.data = NULL, n = 1) {
if(length(n)>1){
n[is.na(n)]<-""
return(paste0(.data, "{", n[1], "," , n[length(n)], "}"))
}
paste0(.data, "{", n,"}")
}
47 changes: 47 additions & 0 deletions R/word.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,50 @@
rx_word <- function(.data = NULL) {
paste0(.data, "\\w+")
}


#' Find beginning or end of a word.
#'
#' @description Match beginning or end of a word—a string consisting of of word characters (a–z, A–Z, 0–9 or _).
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#'
#' @examples
#' rx_word_edge()
#'
#'x <- rx_word_edge() %>%
#' rx_alpha() %>%
#' rx_one_or_more() %>%
#' rx_word_edge()
#'
#'# create inputs
#'string1 <- "foobar"
#'string2 <- "foo 23a bar"
#'
#'# matches 'foobar'
#'regmatches(string1, regexpr(x, string1))
#'# matches 'foo' and 'bar' separately
#'regmatches(string2, gregexpr(x, string2))
#' @export
rx_word_edge <- function(.data = NULL){
paste0(.data, "\\b")
}


#' Match a word character.
#'
#' @description Match a word character (a–z, A–Z, 0–9 or _).
#'
#' @param .data Expression to append, typically pulled from the pipe \code{ \%>\% }
#'
#' @examples
#' rx_word_char()
#'
#' # Same as rx_word()
#'x <- rx_word_char() %>%
#' rx_one_or_more()
#'
#' @export
rx_word_char<- function(.data = NULL){
paste0(.data, "\\w")
}
5 changes: 4 additions & 1 deletion man/rx_count.Rd

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

22 changes: 22 additions & 0 deletions man/rx_word_char.Rd

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

31 changes: 31 additions & 0 deletions man/rx_word_edge.Rd

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

0 comments on commit 2cc018a

Please sign in to comment.