【发布时间】:2021-08-15 11:21:18
【问题描述】:
恐怕我有一个正则表达式问题。我想提取字符串的第一组,即 1 位,省略第二组,即 2 位,然后提取结尾的 5 位作为第三组。
在我看来,它应该看起来像:str_extract(a, "(\\d{1})(\\d{2})(\\d{5})\\1\\3")。但这不起作用。
这里有示例数据,也是想要的结果,但表达方式不同:
library(tidyverse)
d <- tibble(a = as.character(as.integer(runif(10, 1e8, 2e8))) )
d %>%
mutate(want_but_wrong_regex = str_remove(a, "(?<=\\d)\\d{2}")) #
# A tibble: 10 x 2
#a want_but_wrong_regex
#<chr> <chr>
# 1 103016397 1016397
#2 164356395 1356395
#3 134615352 1615352
#4 176581897 1581897
#5 127035705 1035705
#6 158055182 1055182
#7 193991176 1991176
#8 147845896 1845896
#9 177083273 1083273
#10 129086338 1086338
【问题讨论】: