我正在寻找一种方法来删除两个特定字母“bd”和“ls”之前的所有前导字符串。
但是,我只找到了在空格或标点符号之前删除字符串的正则表达式方法。有什么方法可以在特定字母对之前删除前导字符串?
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
输入:
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")
预期结果:
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")