Skip to content

Commit 28b327c

Browse files
committed
Clarify result when optional group doesn't match
1 parent 2e847e7 commit 28b327c

File tree

4 files changed

+31
-75
lines changed

4 files changed

+31
-75
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
str_replace_all(input, c("[ad]" = "!", "[cf]" = "?"))
2323
```
2424

25+
* `str_match()` now returns NA if an optional group doesn't match
26+
(previously it returned ""). This is more consistent with `str_extract()`
27+
and other match failures.
28+
2529
* New `str_subset()` keeps values that match a pattern. It's a convenient
2630
wrapper for `x[str_detect(x)]` (#21, @jiho).
2731

R/match.r

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#' @return For \code{str_match}, a character matrix. First column is the
99
#' complete match, followed by one column for each capture group.
1010
#' For \code{str_match_all}, a list of character matrices.
11+
#'
1112
#' @seealso \code{\link{str_extract}} to extract the complete match,
1213
#' \code{\link[stringi]{stri_match}} for the underlying
1314
#' implementation.
@@ -25,10 +26,17 @@
2526
#' # Extract/match all
2627
#' str_extract_all(strings, phone)
2728
#' str_match_all(strings, phone)
29+
#'
30+
#' x <- c("<a> <b>", "<a> <>", "<a>", "", NA)
31+
#' str_match(x, "<(.*?)> <(.*?)>")
32+
#' str_match_all(x, "<(.*?)>")
33+
#'
34+
#' str_extract(x, "<.*?>")
35+
#' str_extract_all(x, "<.*?>")
2836
str_match <- function(string, pattern) {
2937
switch(type(pattern),
3038
regex = stri_match_first_regex(string, pattern,
31-
opts_regex = attr(pattern, "options"), cg_missing = ""),
39+
opts_regex = attr(pattern, "options")),
3240
stop("Can only match regular expressions", call. = FALSE)
3341
)
3442
}
@@ -38,8 +46,9 @@ str_match <- function(string, pattern) {
3846
str_match_all <- function(string, pattern) {
3947
switch(type(pattern),
4048
regex = stri_match_all_regex(string, pattern,
41-
omit_no_match = TRUE, opts_regex = attr(pattern, "options"),
42-
cg_missing = ""),
49+
cg_missing = "",
50+
omit_no_match = TRUE,
51+
opts_regex = attr(pattern, "options")),
4352
stop("Can only match regular expressions", call. = FALSE)
4453
)
4554
}

revdep/summary.md

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -297,32 +297,7 @@ __OK__
297297
## docopt (0.4.2)
298298
Maintainer: Edwin de Jonge <[email protected]>
299299

300-
```
301-
checking examples ... ERROR
302-
Running examples in ‘docopt-Ex.R’ failed
303-
The error most likely occurred in:
304-
305-
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
306-
> ### Name: docopt
307-
> ### Title: Parse 'args' based on command-line interface described in 'doc'.
308-
> ### Aliases: docopt
309-
>
310-
> ### ** Examples
311-
>
312-
> "Usage: my_program.R [-hso FILE] [--quiet | --verbose] [INPUT ...]
313-
+
314-
+ -h --help show this
315-
+ -s --sorted sorted output
316-
+ -o FILE specify output file [default: ./test.txt]
317-
+ --quiet print less text
318-
+ --verbose print more text" -> doc
319-
> docopt(doc, "-s --quiet")
320-
perl is deprecated. Please use regexp instead
321-
perl is deprecated. Please use regexp instead
322-
Error in parse_long(tokens, optionlist) : is specified ambigously
323-
Calls: docopt -> parse_args -> parse_long -> <Anonymous>
324-
Execution halted
325-
```
300+
__OK__
326301

327302
## dplR (1.6.2)
328303
Maintainer: Andy Bunn <[email protected]>
@@ -338,7 +313,7 @@ Maintainer: John Waller <[email protected]>
338313
```
339314
checking whether package ‘EasyMARK’ can be installed ... ERROR
340315
Installation failed.
341-
See ‘/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/EasyMARK.Rcheck/00install.out’ for details.
316+
See ‘/private/tmp/RtmpPgHCED/check_cran46f2570a2e67/EasyMARK.Rcheck/00install.out’ for details.
342317
```
343318

344319
## eeptools (0.3.1)
@@ -585,7 +560,7 @@ Maintainer: Jose Gerardo Tamez-Pena <[email protected]>
585560
```
586561
checking whether package ‘FRESA.CAD’ can be installed ... ERROR
587562
Installation failed.
588-
See ‘/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/FRESA.CAD.Rcheck/00install.out’ for details.
563+
See ‘/private/tmp/RtmpPgHCED/check_cran46f2570a2e67/FRESA.CAD.Rcheck/00install.out’ for details.
589564
```
590565

591566
## fslr (1.3)
@@ -827,24 +802,6 @@ Bug reports: https://bitbucket.org/djhshih/io/issues
827802
checking package dependencies ... NOTE
828803
Package suggested but not available for checking: ‘rhdf5’
829804
```
830-
```
831-
checking tests ... ERROR
832-
Running the tests in ‘tests/testthat.R’ failed.
833-
Last 13 lines of output:
834-
7 string mismatches
835-
836-
2. Failure(@/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/io.Rcheck/tests/testthat/helper.R#71): write-and-read does not change data
837-
x not equal to y
838-
Names: 2 string mismatches
839-
840-
3. Failure(@/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/io.Rcheck/tests/testthat/helper.R#91): write to text connection does not change content
841-
inlines not equal to textlines
842-
Lengths (7, 8) differ (string compare on first 7)
843-
7 string mismatches
844-
845-
Error: Test failures
846-
Execution halted
847-
```
848805

849806
## ISOweek (0.6-2)
850807
Maintainer: Uwe Block <[email protected]>
@@ -1230,10 +1187,9 @@ The error most likely occurred in:
12301187
> res <- nmf(V, 3)
12311188
>
12321189
> basismap(res)
1233-
Error in ccPalette(x, ...) :
1234-
Invalid palette name '': should be an RColorBrewer palette or one of 'RdYlBu2', 'rainbow', 'heat', 'topo', 'terrain', 'cm', 'gray', 'grey'.
1235-
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'.
1236-
Calls: basismap ... basismap -> .local -> aheatmap -> ccRamp -> ccPalette
1190+
Error in process_tracks(x, tracks, annRow, annCol) :
1191+
Invalid special annotation track name ['basis']. Should partially match one of 'basis', ':basis', 'basis:'.
1192+
Calls: basismap -> basismap -> .local -> process_tracks
12371193
Execution halted
12381194
```
12391195

@@ -1565,7 +1521,7 @@ Bug reports: http://github.com/renozao/RcppOctave/issues
15651521
```
15661522
checking whether package ‘RcppOctave’ can be installed ... ERROR
15671523
Installation failed.
1568-
See ‘/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/RcppOctave.Rcheck/00install.out’ for details.
1524+
See ‘/private/tmp/RtmpPgHCED/check_cran46f2570a2e67/RcppOctave.Rcheck/00install.out’ for details.
15691525
```
15701526

15711527
## rdryad (0.1.1)
@@ -1850,24 +1806,7 @@ manual.
18501806
Maintainer: Simon Potter <[email protected]>
18511807
Bug reports: https://github.com/sjp/selectr/issues
18521808

1853-
```
1854-
checking tests ... ERROR
1855-
Running the tests in ‘tests/test-all.R’ failed.
1856-
Last 13 lines of output:
1857-
y[1]: "Element[]"
1858-
1859-
1860-
1861-
Z. Failure(@test-pseudo.R#36): parser parses canonical pseudo element expressions
1862-
parse_one("*") not equal to list("Element[*]", NULL)
1863-
Component 1: 1 string mismatch
1864-
1865-
Error: Test failures
1866-
In addition: Warning message:
1867-
In mapply(FUN = f, ..., SIMPLIFY = FALSE) :
1868-
longer argument not a multiple of length of shorter
1869-
Execution halted
1870-
```
1809+
__OK__
18711810

18721811
## seqminer (3.4)
18731812
Maintainer: Xiaowei Zhan <[email protected]>
@@ -1886,7 +1825,7 @@ Maintainer: Roger Marshall <[email protected]>
18861825
```
18871826
checking whether package ‘spanr’ can be installed ... ERROR
18881827
Installation failed.
1889-
See ‘/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/spanr.Rcheck/00install.out’ for details.
1828+
See ‘/private/tmp/RtmpPgHCED/check_cran46f2570a2e67/spanr.Rcheck/00install.out’ for details.
18901829
```
18911830

18921831
## spatsurv (0.9-8)
@@ -1925,7 +1864,7 @@ Maintainer: Roger Marshall <[email protected]>
19251864
```
19261865
checking whether package ‘srd’ can be installed ... ERROR
19271866
Installation failed.
1928-
See ‘/private/tmp/RtmpPgHCED/check_cran46f22e9b2abb/srd.Rcheck/00install.out’ for details.
1867+
See ‘/private/tmp/RtmpPgHCED/check_cran46f2570a2e67/srd.Rcheck/00install.out’ for details.
19291868
```
19301869

19311870
## ssh.utils (1.0)

tests/testthat/test-match.r

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test_that("single match works when all match", {
3535
expect_that(matches_flat, equals(num_flat))
3636
})
3737

38-
test_that("single match works when some don't match", {
38+
test_that("match returns NA when some inputs don't match", {
3939
matches <- str_match(c(phones, "blah", NA),
4040
"\\(([0-9]{3})\\) ([0-9]{3}) ([0-9]{4})")
4141

@@ -46,6 +46,10 @@ test_that("single match works when some don't match", {
4646
expect_that(matches[12, ], equals(rep(NA_character_, 4)))
4747
})
4848

49+
test_that("match returns NA when optional group doesn't match", {
50+
expect_equal(str_match(c("ab", "a"), "(a)(b)?")[,3], c("b", NA))
51+
})
52+
4953
test_that("multiple match works", {
5054
phones_one <- str_c(phones, collapse = " ")
5155
multi_match <- str_match_all(phones_one,

0 commit comments

Comments
 (0)