Remove all leading string before two specific letters in R

I am looking for a way to remove all leading strings before two specific letters "bd" and "ls".

However, I only found regex ways to remove string before white space or punctuation. Are there any ways to remove leading string before pairs of specific letters?

      date_on                          location
14 2021-02-22 bradford, west yorkshire, bd9 6dp
15 2021-02-22                     bradford, bd4
16 2021-02-22          bradford, west yorkshire
17 2021-02-22           west yorkshire, bd1 1nq
18 2021-02-22          bradford, west yorkshire
19 2021-02-22                          ls28 7he

dput:

structure(list(date_on = structure(c(18680, 18680, 18680, 18680, 
18680, 18680), class = "Date"), location = c("bradford, west yorkshire, bd9 6dp", 
"bradford, bd4", "bradford, west yorkshire", "west yorkshire, bd1 1nq", 
"bradford, west yorkshire", "ls28 7he")), row.names = 14:19, class = "data.frame")

expected outcome:

      date_on location
14 2021-02-22  bd9 6dp
15 2021-02-22      bd4
16 2021-02-22         
17 2021-02-22  bd1 1nq
18 2021-02-22         
19 2021-02-22 ls28 7he
structure(list(date_on = structure(c(18680, 18680, 18680, 18680, 
18680, 18680), class = "Date"), location = c("bd9 6dp", 
"bd4", "", "bd1 1nq", "", "ls28 7he")), row.names = 14:19, class = "data.frame")


Read more here: https://stackoverflow.com/questions/66343535/remove-all-leading-string-before-two-specific-letters-in-r

Content Attribution

This content was originally published by Gnin at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: