Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Just Quickly: What I usually want from stringr | Credibly Curious #76

Open
utterances-bot opened this issue Jan 5, 2024 · 2 comments
Open

Comments

@utterances-bot
Copy link

Just Quickly: What I usually want from stringr | Credibly Curious

https://www.njtierney.com/post/2019/06/19/quick-stringr/

Copy link

slager commented Jan 5, 2024

A tricky thing is that the period is a special wildcard character in R regular expressions. When the goal is to filter on a filename extension, escaping it is the safest way to go:

library(stringr)
items2 <- c("thing1",
            "notcsv",
            "sacvy",
            "item.csv",
            "wat.csv")
items2[grepl(".csv$", items2)]
#> [1] "notcsv"   "item.csv" "wat.csv"
str_subset(items2, ".csv$")
#> [1] "notcsv"   "item.csv" "wat.csv"
items2[grepl("\\.csv$", items2)]
#> [1] "item.csv" "wat.csv"
str_subset(items2, "\\.csv$")
#> [1] "item.csv" "wat.csv"

@njtierney
Copy link
Collaborator

good point about the wildcard aspect! I guess I had internalised that it wouldn't be so bad if the wildcard was used, but I can see from your examples that this could be bad indeed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants