Git Product home page Git Product logo

Comments (2)

johncassil avatar johncassil commented on July 30, 2024

Oooo! This is fun. Thanks! I really appreciate you taking the time to read the code!

I have heard of and maybe used lookaheads and lookbefores a couple of times. I knew they were an option, but just went with what I had more experience using.

Couple questions for you, since you know more about package maintenance than I do. Can you explain a bit about how it might be more maintainable? Is this in case stringr::str_locate() changes down the road?

I'm thinking if I paired your solution there with grep instead of str_extract and paste0 rather than glue, there would be no non-base dependencies, right?

from stringr.plus.

jonocarroll avatar jonocarroll commented on July 30, 2024

You could certainly do that...

str_before <- function(string, pattern, n = NULL) {
  n_str <- ifelse(is.null(n), "(^.*?", paste0("^.*?(.{", n, "}"))
  new_pattern <- paste0(n_str, ")(?=", pattern, ").*$")
  sub(new_pattern, "\\1", string, perl = TRUE)
}
url <- "www.carfax.com/vehicle/3GCPKTE77DG348900"
str_before(url, "/")
#> [1] "www.carfax.com"
str_before(url, ".com")
#> [1] "www.carfax"
str_before(url, ".com", 6)
#> [1] "carfax"

str_after <- function(string, pattern, n = NULL) {
  n_str <- ifelse(is.null(n), "(.*$)", paste0("(.{", n, "}).*?$"))
  new_pattern <- paste0("^.*?(?<=", pattern, ")", n_str)
  sub(new_pattern, "\\1", string, perl = TRUE)
}
url <- "www.carfax.com/vehicle/3GCPKTE77DG348900"
str_after(url, "/")
#> [1] "vehicle/3GCPKTE77DG348900"
str_after(url, "vehicle/")
#> [1] "3GCPKTE77DG348900"
str_after(url, "vehicle/", 6)
#> [1] "3GCPKT"

Created on 2020-08-16 by the reprex package (v0.3.0)

By "easier to maintain" I mean the effort required to ensure that you don't introduce any new bugs. By using the {stringr} functions you rely on their testing - in my first example I'm introducing very little new code so few places it could go wrong that won't already go wrong in {stringr}. This version has more complexity, but fewer dependencies, so that's a tradeoff. The more expressions you add to a function, the more places for bugs to hide.

In this case you're not benefiting from the nice wrappers that {stringr} has, another tradeoff.

These are useful patterns, so ironing out the wrinkles and making them available is of great benefit.

from stringr.plus.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.