diff --git a/NEWS.md b/NEWS.md index 5bba85d0..7979682a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,6 +22,10 @@ str_replace_all(input, c("[ad]" = "!", "[cf]" = "?")) ``` +* `str_match()` now returns NA if an optional group doesn't match + (previously it returned ""). This is more consistent with `str_extract()` + and other match failures. + * New `str_subset()` keeps values that match a pattern. It's a convenient wrapper for `x[str_detect(x)]` (#21, @jiho). diff --git a/R/match.r b/R/match.r index 3f1f032b..03846c75 100644 --- a/R/match.r +++ b/R/match.r @@ -8,6 +8,7 @@ #' @return For \code{str_match}, a character matrix. First column is the #' complete match, followed by one column for each capture group. #' For \code{str_match_all}, a list of character matrices. +#' #' @seealso \code{\link{str_extract}} to extract the complete match, #' \code{\link[stringi]{stri_match}} for the underlying #' implementation. @@ -25,10 +26,17 @@ #' # Extract/match all #' str_extract_all(strings, phone) #' str_match_all(strings, phone) +#' +#' x <- c(" ", " <>", "", "", NA) +#' str_match(x, "<(.*?)> <(.*?)>") +#' str_match_all(x, "<(.*?)>") +#' +#' str_extract(x, "<.*?>") +#' str_extract_all(x, "<.*?>") str_match <- function(string, pattern) { switch(type(pattern), regex = stri_match_first_regex(string, pattern, - opts_regex = attr(pattern, "options"), cg_missing = ""), + opts_regex = attr(pattern, "options")), stop("Can only match regular expressions", call. = FALSE) ) } @@ -38,8 +46,9 @@ str_match <- function(string, pattern) { str_match_all <- function(string, pattern) { switch(type(pattern), regex = stri_match_all_regex(string, pattern, - omit_no_match = TRUE, opts_regex = attr(pattern, "options"), - cg_missing = ""), + cg_missing = "", + omit_no_match = TRUE, + opts_regex = attr(pattern, "options")), stop("Can only match regular expressions", call. = FALSE) ) } diff --git a/revdep/summary.md b/revdep/summary.md index 46ffb85b..72b8c3af 100644 --- a/revdep/summary.md +++ b/revdep/summary.md @@ -297,32 +297,7 @@ __OK__ ## docopt (0.4.2) Maintainer: Edwin de Jonge -``` -checking examples ... ERROR -Running examples in ‘docopt-Ex.R’ failed -The error most likely occurred in: - -> base::assign(".ptime", proc.time(), pos = "CheckExEnv") -> ### Name: docopt -> ### Title: Parse 'args' based on command-line interface described in 'doc'. -> ### Aliases: docopt -> -> ### ** Examples -> -> "Usage: my_program.R [-hso FILE] [--quiet | --verbose] [INPUT ...] -+ -+ -h --help show this -+ -s --sorted sorted output -+ -o FILE specify output file [default: ./test.txt] -+ --quiet print less text -+ --verbose print more text" -> doc -> docopt(doc, "-s --quiet") -perl is deprecated. Please use regexp instead -perl is deprecated. Please use regexp instead -Error in parse_long(tokens, optionlist) : is specified ambigously -Calls: docopt -> parse_args -> parse_long -> -Execution halted -``` +__OK__ ## dplR (1.6.2) Maintainer: Andy Bunn @@ -338,7 +313,7 @@ Maintainer: John Waller ``` checking whether package ‘EasyMARK’ can be installed ... ERROR Installation failed. -See ‘/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/EasyMARK.Rcheck/00install.out’ for details. +See ‘/private/tmp/RtmpPgHCED/check_cran46f2570a2e67/EasyMARK.Rcheck/00install.out’ for details. ``` ## eeptools (0.3.1) @@ -585,7 +560,7 @@ Maintainer: Jose Gerardo Tamez-Pena ``` checking whether package ‘FRESA.CAD’ can be installed ... ERROR Installation failed. -See ‘/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/FRESA.CAD.Rcheck/00install.out’ for details. +See ‘/private/tmp/RtmpPgHCED/check_cran46f2570a2e67/FRESA.CAD.Rcheck/00install.out’ for details. ``` ## fslr (1.3) @@ -827,24 +802,6 @@ Bug reports: https://bitbucket.org/djhshih/io/issues checking package dependencies ... NOTE Package suggested but not available for checking: ‘rhdf5’ ``` -``` -checking tests ... ERROR -Running the tests in ‘tests/testthat.R’ failed. -Last 13 lines of output: - 7 string mismatches - - 2. Failure(@/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/io.Rcheck/tests/testthat/helper.R#71): write-and-read does not change data - x not equal to y - Names: 2 string mismatches - - 3. Failure(@/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/io.Rcheck/tests/testthat/helper.R#91): write to text connection does not change content - inlines not equal to textlines - Lengths (7, 8) differ (string compare on first 7) - 7 string mismatches - - Error: Test failures - Execution halted -``` ## ISOweek (0.6-2) Maintainer: Uwe Block @@ -1230,10 +1187,9 @@ The error most likely occurred in: > res <- nmf(V, 3) > > basismap(res) -Error in ccPalette(x, ...) : - Invalid palette name '': should be an RColorBrewer palette or one of 'RdYlBu2', 'rainbow', 'heat', 'topo', 'terrain', 'cm', 'gray', 'grey'. - Available RColorBrewer palettes: 'BrBG', 'PiYG', 'PRGn', 'PuOr', 'RdBu', 'RdGy', 'RdYlBu', 'RdYlGn', 'Spectral', 'Accent', 'Dark2', 'Paired', 'Pastel1', 'Pastel2', 'Set1', 'Set2', 'Set3', 'Blues', 'BuGn', 'BuPu', 'GnBu', 'Greens', 'Greys', 'Oranges', 'OrRd', 'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu', 'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd'. -Calls: basismap ... basismap -> .local -> aheatmap -> ccRamp -> ccPalette +Error in process_tracks(x, tracks, annRow, annCol) : + Invalid special annotation track name ['basis']. Should partially match one of 'basis', ':basis', 'basis:'. +Calls: basismap -> basismap -> .local -> process_tracks Execution halted ``` @@ -1565,7 +1521,7 @@ Bug reports: http://github.com/renozao/RcppOctave/issues ``` checking whether package ‘RcppOctave’ can be installed ... ERROR Installation failed. -See ‘/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/RcppOctave.Rcheck/00install.out’ for details. +See ‘/private/tmp/RtmpPgHCED/check_cran46f2570a2e67/RcppOctave.Rcheck/00install.out’ for details. ``` ## rdryad (0.1.1) @@ -1850,24 +1806,7 @@ manual. Maintainer: Simon Potter Bug reports: https://github.com/sjp/selectr/issues -``` -checking tests ... ERROR -Running the tests in ‘tests/test-all.R’ failed. -Last 13 lines of output: - y[1]: "Element[]" - - - - Z. Failure(@test-pseudo.R#36): parser parses canonical pseudo element expressions - parse_one("*") not equal to list("Element[*]", NULL) - Component 1: 1 string mismatch - - Error: Test failures - In addition: Warning message: - In mapply(FUN = f, ..., SIMPLIFY = FALSE) : - longer argument not a multiple of length of shorter - Execution halted -``` +__OK__ ## seqminer (3.4) Maintainer: Xiaowei Zhan @@ -1886,7 +1825,7 @@ Maintainer: Roger Marshall ``` checking whether package ‘spanr’ can be installed ... ERROR Installation failed. -See ‘/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/spanr.Rcheck/00install.out’ for details. +See ‘/private/tmp/RtmpPgHCED/check_cran46f2570a2e67/spanr.Rcheck/00install.out’ for details. ``` ## spatsurv (0.9-8) @@ -1925,7 +1864,7 @@ Maintainer: Roger Marshall ``` checking whether package ‘srd’ can be installed ... ERROR Installation failed. -See ‘/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/srd.Rcheck/00install.out’ for details. +See ‘/private/tmp/RtmpPgHCED/check_cran46f2570a2e67/srd.Rcheck/00install.out’ for details. ``` ## ssh.utils (1.0) diff --git a/tests/testthat/test-match.r b/tests/testthat/test-match.r index 28ba2b14..33c31e2d 100644 --- a/tests/testthat/test-match.r +++ b/tests/testthat/test-match.r @@ -35,7 +35,7 @@ test_that("single match works when all match", { expect_that(matches_flat, equals(num_flat)) }) -test_that("single match works when some don't match", { +test_that("match returns NA when some inputs don't match", { matches <- str_match(c(phones, "blah", NA), "\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})") @@ -46,6 +46,10 @@ test_that("single match works when some don't match", { expect_that(matches[12, ], equals(rep(NA_character_, 4))) }) +test_that("match returns NA when optional group doesn't match", { + expect_equal(str_match(c("ab", "a"), "(a)(b)?")[,3], c("b", NA)) +}) + test_that("multiple match works", { phones_one <- str_c(phones, collapse = " ") multi_match <- str_match_all(phones_one,