Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created July 16, 2025 02:18
Show Gist options
  • Save njtierney/0f9d9002ee460edcdb13322496a311f9 to your computer and use it in GitHub Desktop.
Save njtierney/0f9d9002ee460edcdb13322496a311f9 to your computer and use it in GitHub Desktop.
library(stringr)
str_remove(str_extract("65. A question", "\\d+\\."), "\\.")
#> [1] "65"

str_extract_question_number <- function(text){
  str_remove(str_extract(text, "\\d+\\."), "\\.")
}

a_string <- "65. A question with other numbers, 123"
str_extract_question_number(a_string)
#> [1] "65"
# alternatively - but it is still an integer
readr::parse_number(a_string)
#> [1] 65

bm <- bench::mark(
  
  stringr = str_extract_question_number(a_string),
  # alternatively - but it is still an integer
  readr = as.character(readr::parse_number(a_string))
)

bm
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 stringr      43.1µs   45.8µs    20329.    8.52KB     18.5
#> 2 readr        27.8µs   30.3µs    33108.        0B     19.9
summary(bm, relative = TRUE)
#> # A tibble: 2 × 6
#>   expression   min median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <dbl>  <dbl>     <dbl>     <dbl>    <dbl>
#> 1 stringr     1.55   1.51      1          Inf     1   
#> 2 readr       1      1         1.63       NaN     1.08

Created on 2025-07-16 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.5.1 (2025-06-13)
#>  os       macOS Sonoma 14.5
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Australia/Brisbane
#>  date     2025-07-16
#>  pandoc   3.4 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#>  quarto   1.7.31 @ /usr/local/bin/quarto
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  bench         1.1.4   2025-01-16 [1] CRAN (R 4.5.0)
#>  cli           3.6.5   2025-04-23 [1] CRAN (R 4.5.0)
#>  digest        0.6.37  2024-08-19 [1] CRAN (R 4.5.0)
#>  evaluate      1.0.4   2025-06-18 [1] CRAN (R 4.5.0)
#>  fastmap       1.2.0   2024-05-15 [1] CRAN (R 4.5.0)
#>  fs            1.6.6   2025-04-12 [1] CRAN (R 4.5.0)
#>  glue          1.8.0   2024-09-30 [1] CRAN (R 4.5.0)
#>  hms           1.1.3   2023-03-21 [1] CRAN (R 4.5.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.5.0)
#>  knitr         1.50    2025-03-16 [1] CRAN (R 4.5.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.5.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.5.0)
#>  pillar        1.11.0  2025-07-04 [1] CRAN (R 4.5.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.5.0)
#>  profmem       0.6.0   2020-12-13 [1] CRAN (R 4.5.0)
#>  R6            2.6.1   2025-02-15 [1] CRAN (R 4.5.0)
#>  readr         2.1.5   2024-01-10 [1] CRAN (R 4.5.0)
#>  reprex        2.1.1   2024-07-06 [1] CRAN (R 4.5.0)
#>  rlang         1.1.6   2025-04-11 [1] CRAN (R 4.5.0)
#>  rmarkdown     2.29    2024-11-04 [1] CRAN (R 4.5.0)
#>  rstudioapi    0.17.1  2024-10-22 [1] CRAN (R 4.5.0)
#>  sessioninfo   1.2.3   2025-02-05 [1] CRAN (R 4.5.0)
#>  stringi       1.8.7   2025-03-27 [1] CRAN (R 4.5.0)
#>  stringr     * 1.5.1   2023-11-14 [1] CRAN (R 4.5.0)
#>  tibble        3.3.0   2025-06-08 [1] CRAN (R 4.5.0)
#>  tzdb          0.5.0   2025-03-15 [1] CRAN (R 4.5.0)
#>  utf8          1.2.6   2025-06-08 [1] CRAN (R 4.5.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.5.0)
#>  withr         3.0.2   2024-10-28 [1] CRAN (R 4.5.0)
#>  xfun          0.52    2025-04-02 [1] CRAN (R 4.5.0)
#>  yaml          2.3.10  2024-07-26 [1] CRAN (R 4.5.0)
#> 
#>  [1] /Users/nick_1/Library/R/arm64/4.5/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/library
#>  * ── Packages attached to the search path.
#> 
#> ──────────────────────────────────────────────────────────────────────────────
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment