在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]

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

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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在java中使用正则表达式从字符串中提取字符串值

在python中使用正则表达式从大字符串中提取字符串列表

在大熊猫中使用正则表达式从多个括号中提取字符串

Java:使用正则表达式从输入中提取字符串

如何使用正则表达式从列表中提取字符串匹配项?

PHP /正则表达式:从字符串中提取字符串

正则表达式:从字符串中提取字符串

在python正则表达式中提取字符串的子字符串

在Oracle中使用正则表达式提取字符串部分

在Python中使用正则表达式提取字符串的特定部分

在linux中使用正则表达式提取字符串的子集

使用正则表达式从Python字符串中提取超链接文本

如何使用正则表达式从R中的模式提取字符串

在bash中使用正则表达式从字符串中提取信息

在Scala中使用正则表达式从字符串中提取所有标记

在Java中使用正则表达式从URL的字符串组中提取数字

在python中使用正则表达式从字符串中提取数字

在Python中使用正则表达式从字符串中提取坐标

在 JavaScript 中使用正则表达式从字符串中提取键值

在 Python 中使用正则表达式从字符串中提取数字数据

如何在java中使用正则表达式从字符串中提取数字

从仅与正则表达式模式匹配的字符串中提取字符

正则表达式提取字符串

正则表达式提取字符串

正则表达式提取字符串

提取字符串正则表达式

正则表达式从url中提取字符串

从正则表达式中提取字符串

正则表达式匹配后如何从数据框中提取字符串