We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent faedc2d commit f70c4baCopy full SHA for f70c4ba
NEWS.md
@@ -1,5 +1,7 @@
1
# stringr (development version)
2
3
+* Update `str_starts()` and `str_ends()` functions so they honor regex operator precedence. (@carlganz)
4
+
5
* `word()` now returns all the sentence when using a negative `start` parameter
6
that is greater or equal than the number of words. (@pdelboca, #245)
7
R/detect.r
@@ -82,7 +82,7 @@ str_starts <- function(string, pattern, negate = FALSE) {
82
fixed = stri_startswith_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)),
83
coll = stri_startswith_coll(string, pattern, negate = negate, opts_collator = opts(pattern)),
84
regex = {
85
- pattern2 <- paste0("^", pattern)
+ pattern2 <- paste0("^(", pattern, ")")
86
attributes(pattern2) <- attributes(pattern)
87
str_detect(string, pattern2, negate)
88
}
@@ -98,7 +98,7 @@ str_ends <- function(string, pattern, negate = FALSE) {
98
fixed = stri_endswith_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)),
99
coll = stri_endswith_coll(string, pattern, negate = negate, opts_collator = opts(pattern)),
100
101
- pattern2 <- paste0(pattern, "$")
+ pattern2 <- paste0("(", pattern, ")$")
102
103
104
tests/testthat/test-detect.r
@@ -36,6 +36,10 @@ test_that("str_starts works", {
36
# Special typing of patterns.
37
expect_true(str_starts("ab", fixed("A", ignore_case = TRUE)))
38
expect_true(str_starts("ab", regex("A", ignore_case = TRUE)))
39
40
+ # Or operators are respected
41
+ expect_true(str_starts("ab", "b|a"))
42
+ expect_false(str_starts("ab", "c|b"))
43
})
44
45
test_that("str_ends works", {
@@ -48,6 +52,10 @@ test_that("str_ends works", {
48
52
49
53
50
54
expect_true(str_ends("ab", fixed("B", ignore_case = TRUE)))
55
56
57
+ expect_true(str_ends("ab", "b|a"))
58
+ expect_false(str_ends("ab", "c|a"))
51
59
60
61
0 commit comments