Skip to content

Commit

Permalink
Clarify result when optional group doesn't match
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Apr 15, 2015
1 parent 2e847e7 commit 28b327c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 75 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Expand Up @@ -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).

Expand Down
15 changes: 12 additions & 3 deletions R/match.r
Expand Up @@ -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.
Expand All @@ -25,10 +26,17 @@
#' # Extract/match all
#' str_extract_all(strings, phone)
#' str_match_all(strings, phone)
#'
#' x <- c("<a> <b>", "<a> <>", "<a>", "", 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)
)
}
Expand All @@ -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)
)
}
81 changes: 10 additions & 71 deletions revdep/summary.md
Expand Up @@ -297,32 +297,7 @@ __OK__
## docopt (0.4.2)
Maintainer: Edwin de Jonge <edwindjonge@gmail.com>

```
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 -> <Anonymous>
Execution halted
```
__OK__

## dplR (1.6.2)
Maintainer: Andy Bunn <andy.bunn@wwu.edu>
Expand All @@ -338,7 +313,7 @@ Maintainer: John Waller <john.waller@biol.lu.se>
```
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)
Expand Down Expand Up @@ -585,7 +560,7 @@ Maintainer: Jose Gerardo Tamez-Pena <jose.tamezpena@itesm.mx>
```
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)
Expand Down Expand Up @@ -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 <u.block.mz@googlemail.com>
Expand Down Expand Up @@ -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
```

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1850,24 +1806,7 @@ manual.
Maintainer: Simon Potter <simon@sjp.co.nz>
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 <zhanxw@gmail.com>
Expand All @@ -1886,7 +1825,7 @@ Maintainer: Roger Marshall <rj.marshall@auckland.ac.nz>
```
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)
Expand Down Expand Up @@ -1925,7 +1864,7 @@ Maintainer: Roger Marshall <rj.marshall@auckland.ac.nz>
```
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)
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-match.r
Expand Up @@ -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})")

Expand All @@ -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,
Expand Down

0 comments on commit 28b327c

Please sign in to comment.