Skip to content

Commit

Permalink
udpated str_extract() and added str_extract_all()
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed Jun 6, 2024
1 parent 70aeb4f commit 530c5ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
33 changes: 9 additions & 24 deletions R/standalone-stringr.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,6 @@ str_squish <- function(string, fixed = FALSE) {
return(string)
}


str_extract <- function(string, pattern, fixed = FALSE) {
result <- sapply(string, function(x) {
# Adjust the pattern if fixed is TRUE
if (fixed) {
# Escape special characters to treat them as literal in regex
pattern <- gsub("([][{}()+*^$.|\\\\?])", "\\\\\\1", pattern)
}
match_pos <- regexpr(pattern = pattern, text = x, perl = TRUE)
if (match_pos > 0) {
regmatches(x, match_pos)
} else {
NA_character_
}
}, USE.NAMES = FALSE)

return(result)
}

str_remove <- function (string, pattern, fixed = FALSE) {
sub (x = string, pattern = pattern, replacement = "", fixed = fixed)
}
Expand All @@ -53,11 +34,15 @@ str_remove_all <- function(string, pattern, fixed = FALSE) {
}

str_extract <- function(string, pattern, fixed = FALSE) {
ifelse(
str_detect(string, pattern, fixed = fixed),
regmatches(x = string, m = regexpr(pattern = pattern, text = string, fixed = fixed)),
NA_character_
)
res <- rep(NA_character_, length.out = length(string))
res[str_detect(string, pattern, fixed = fixed)] <-
regmatches(x = string, m = regexpr(pattern = pattern, text = string, fixed = fixed))

res
}

str_extract_all <- function(string, pattern, fixed = FALSE) {
regmatches(x = string, m = gregexpr(pattern = pattern, text = string, fixed = fixed))
}

str_detect <- function(string, pattern, fixed = FALSE) {
Expand Down
21 changes: 20 additions & 1 deletion tests/testthat/test-standalone-stringr.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test_that("str_remove_all() works", {
})

test_that("str_extract() works", {
shopping_list <- c("apples x43", "bag of flour", "bag of sugar", "milk x2")
shopping_list <- c("apples x43", "bag of flour", "bag of sugar, bag of sugar", "milk x2")

s <- str_extract(shopping_list, "[a-z]+")
expect_identical(s, c("apples", "bag", "bag", "milk"))
Expand All @@ -81,6 +81,25 @@ test_that("str_extract() works", {
expect_identical(s, stringr::str_extract(shopping_list, "[a-z]+"))
})

test_that("str_extract_all() works", {
shopping_list <- c("apples x43", "bag of flour", "bag of sugar, bag of sugar", "milk x2")

s <- str_extract_all(shopping_list, "[a-z]+")
expect_identical(s, stringr::str_extract_all(shopping_list, "[a-z]+"))

s <- str_extract_all(shopping_list, "([a-z]+) of ([a-z]+)")
expect_identical(s, stringr::str_extract_all(shopping_list, "([a-z]+) of ([a-z]+)"))

s_notfixed <- str_extract_all(shopping_list, "\\d")
expect_identical(s_notfixed, stringr::str_extract_all(shopping_list, "\\d"))

s_fixed <- str_extract_all(shopping_list, "\\d", fixed = TRUE)
expect_identical(s_fixed, stringr::str_extract_all(shopping_list, stringr::fixed("\\d")))

s <- str_extract_all(shopping_list, "[a-z]+")
expect_identical(s, stringr::str_extract_all(shopping_list, "[a-z]+"))
})

test_that("str_detect() works", {
fruits <- c("apple", "banana", "pear", "pineapple")

Expand Down

0 comments on commit 530c5ea

Please sign in to comment.