在 R 中使用正则表达式从链接中提取字符串

Extracting strings from links using regex in R

我有一个 url 链接列表,我想提取其中一个字符串并将它们保存在另一个变量中。示例数据如下:

  sample<-  c("http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr01f2009.pdf",
            "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr02f2001.pdf",
           "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr03f2002.pdf",
          "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr04f2004.pdf",
         "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr05f2005.pdf",
           "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr06f2018.pdf",
           "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr07f2016.pdf",
            "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr08f2015.pdf",
          "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr09f2020.pdf",
             "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr10f2014.pdf")

sample

 [1] "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr01f2009.pdf"
 [2] "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr02f2001.pdf"
 [3] "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr03f2002.pdf"
 [4] "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr04f2004.pdf"
 [5] "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr05f2005.pdf"
 [6] "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr06f2018.pdf"
 [7] "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr07f2016.pdf"
 [8] "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr08f2015.pdf"
 [9] "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr09f2020.pdf"
[10] "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr10f2014.pdf"

我想使用正则表达式提取周和年。

     week year
1     1 2009
2     2 2001
3     3 2002
4     4 2004
5     5 2005
6     6 2018
7     7 2016
8     8 2015
9     9 2020
10   10 2014

您可以使用 str_match 来捕获 'owgr''f' 之后的数字:

library(stringr)
str_match(sample, 'owgr(\d+)f(\d+)')[, -1]

您可以将其转换为数据框,将 class 更改为数字并分配列名。

setNames(type.convert(data.frame(
          str_match(sample, 'owgr(\d+)f(\d+)')[, -1])), c('year', 'week'))

#   year week
#1     1 2009
#2     2 2001
#3     3 2002
#4     4 2004
#5     5 2005
#6     6 2018
#7     7 2016
#8     8 2015
#9     9 2020
#10   10 2014

另一种方法是提取 sample 最后一部分的所有数字。我们可以用 basename.

得到最后一部分
str_extract_all(basename(sample), '\d+', simplify = TRUE)

您可以尝试另一种方法

library(dplyr)
library(stringr)
df <- data.frame(sample)
df2 <- df %>% 
  transmute(year = str_extract(sample, "(?<=wgr)\d{1,2}(?=f)"), week = str_extract(sample, "(?<=f)\d{4}(?=\.pdf)"))

#     year week
# 1     1 2009
# 2     2 2001
# 3     3 2002
# 4     4 2004
# 5     5 2005
# 6     6 2018
# 7     7 2016
# 8     8 2015
# 9     9 2020
# 10   10 2014

你可以使用 {unglue} :

library(unglue)

unglue_data(
  sample,
  "http://dps.endavadigital.net/owgr/doc/content/archive/2009/owgr{week}f{year}.pdf")
#>    week year
#> 1    01 2009
#> 2    02 2001
#> 3    03 2002
#> 4    04 2004
#> 5    05 2005
#> 6    06 2018
#> 7    07 2016
#> 8    08 2015
#> 9    09 2020
#> 10   10 2014