Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式:提取url的一部分并在r中创建新列_R_Regex_Url - Fatal编程技术网

正则表达式:提取url的一部分并在r中创建新列

正则表达式:提取url的一部分并在r中创建新列,r,regex,url,R,Regex,Url,我有URL的记录,我想提取其中的一部分并创建新的列。在我的例子中,我想考虑“代码”>“组” >代码>组pII/和 DICUSONSONTHOMENT/ AS 讨论> ID > 类似于: 我想要这样的结果 user group_id dicussion_id 1 3276 3939 2 34 11 3 3276 NA 4 NA NA 如何使用R中的正则表达式来实现它? thx另一个带有str

我有URL的记录,我想提取其中的一部分并创建新的列。在我的例子中,我想考虑“代码”>“组”<代码> >代码>组pII/<代码>和<代码> DICUSONSONTHOMENT/<代码> AS <代码>讨论> ID <代码> > 类似于:

我想要这样的结果

user  group_id  dicussion_id  
1      3276       3939
2      34         11
3      3276       NA
4      NA         NA
如何使用R中的正则表达式来实现它?
thx

另一个带有
stringi
包和lookbehind regex的版本

更新:无可否认,@onyanbu的功能更快。参见基准。 更新2:在基准测试中添加了第三个版本。速度没有提高

library(stringi)
extract_info = function(x) {
  x$group = stri_extract_all_regex(x$url, "(?<=groups/)\\d+")
  x$topic = stri_extract_all_regex(x$url, "(?<=discussion_topics/)\\d+")
  x
}
extract_info(dat)
#    user                                                 url group topic
# 1    1 https://test.com/groups/3276/discussion_topics/3939  3276  3939
# 2    2     https://test.com/groups/34/discussion_topics/11    34    11
# 3    3                        https://test.com/groups/3276  3276    NA
# 4    4                       https://test.com/groups/other    NA    NA

extract_info2 = function(dat) {
dat$group_id=as.numeric(sub(".*/groups/(\\d+).*|.*","\\1",dat$url))
dat$discussion=as.numeric(sub(".*/discussion_topics/(\\d+).*|.*","\\1",dat$url))
dat
}

extract_info3 = function(data) {
  df$group_id <- as.numeric(regmatches(df$url, gregexpr(".*groups/*\\K.\\d+", df$url, perl=TRUE)))
  df$discussion <- as.numeric(regmatches(df$url, gregexpr(".*topics/*\\K.\\d+", df$url, perl=TRUE)))
  df
}

microbenchmark::microbenchmark(
  extract_info(dat)
  ,extract_info2(dat)
  ,extract_info3(dat)
)
# Unit: microseconds
#            expr     min      lq     mean   median       uq      max neval
# extract_info(dat)  152.769 160.269 172.1629 170.5325 176.0590  300.011   100
# extract_info2(dat)  99.872 106.386 120.9876 117.2415 125.7285  226.981   100
# extract_info3(dat) 285.799 301.984 378.7235 308.8925 323.3000 6684.297   100
库(stringi)
提取信息=函数(x){

x$group=stri_extract_all_regex(x$url,”(?另一个版本,带有
stringi
包和lookback regex

更新:不可否认,@onyanbu的功能更快。参见基准测试。 更新2:在基准测试中添加了第三个版本。在速度方面没有任何改进

library(stringi)
extract_info = function(x) {
  x$group = stri_extract_all_regex(x$url, "(?<=groups/)\\d+")
  x$topic = stri_extract_all_regex(x$url, "(?<=discussion_topics/)\\d+")
  x
}
extract_info(dat)
#    user                                                 url group topic
# 1    1 https://test.com/groups/3276/discussion_topics/3939  3276  3939
# 2    2     https://test.com/groups/34/discussion_topics/11    34    11
# 3    3                        https://test.com/groups/3276  3276    NA
# 4    4                       https://test.com/groups/other    NA    NA

extract_info2 = function(dat) {
dat$group_id=as.numeric(sub(".*/groups/(\\d+).*|.*","\\1",dat$url))
dat$discussion=as.numeric(sub(".*/discussion_topics/(\\d+).*|.*","\\1",dat$url))
dat
}

extract_info3 = function(data) {
  df$group_id <- as.numeric(regmatches(df$url, gregexpr(".*groups/*\\K.\\d+", df$url, perl=TRUE)))
  df$discussion <- as.numeric(regmatches(df$url, gregexpr(".*topics/*\\K.\\d+", df$url, perl=TRUE)))
  df
}

microbenchmark::microbenchmark(
  extract_info(dat)
  ,extract_info2(dat)
  ,extract_info3(dat)
)
# Unit: microseconds
#            expr     min      lq     mean   median       uq      max neval
# extract_info(dat)  152.769 160.269 172.1629 170.5325 176.0590  300.011   100
# extract_info2(dat)  99.872 106.386 120.9876 117.2415 125.7285  226.981   100
# extract_info3(dat) 285.799 301.984 378.7235 308.8925 323.3000 6684.297   100
库(stringi)
提取信息=函数(x){

x$group=stri_extract_all_regex(x$url),(?这里是另一个选项:

df$group_id <- as.numeric(regmatches(df$url, gregexpr(".*groups/*\\K.\\d+", df$url, perl=TRUE)))
df$discussion <- as.numeric(regmatches(df$url, gregexpr(".*topics/*\\K.\\d+", df$url, perl=TRUE)))

df$group\u id这里是另一个选项:

df$group_id <- as.numeric(regmatches(df$url, gregexpr(".*groups/*\\K.\\d+", df$url, perl=TRUE)))
df$discussion <- as.numeric(regmatches(df$url, gregexpr(".*topics/*\\K.\\d+", df$url, perl=TRUE)))

df$group\u id非常感谢您,您能解释一下\\d和\\1吗?\d代表数字,+代表1或更多;\d被括在括号中,标记为捕获组;\1是要提取的第一个捕获组的反向引用。如果有多个这样的组,您可以提取这两个组,例如,按字符串中的切换顺序\2\1.您可能需要查看或类似页面以了解更多详细信息。@Cina请查看Manuel的回答谢谢,您能解释一下\\d和\\1吗?\d代表数字,+代表1或更多;\d用括号括起来,标记为捕获组;\1是要提取的第一个捕获组的反向引用。如果有更多不止一个这样的组,您可以同时提取这两个组,例如,以字符串by\2\1的切换顺序。您可以查看或类似的页面以了解更多详细信息。@Cina请查看Manuel的ResponseGraat thx。我不明白为什么正则表达式在每个应答器中都不同。使用正则表达式时,您必须根据您使用的字符串指定合适的模式各种模式可能适用于获取您想要的内容,例如,要从“101abc101”中提取“abc”,您可以通过gsub(\\d,“,…)删除所有数字,或者使用捕获组gsub((^.*)(abc)(.*$),“\\2”,…)提取字母序列,或者…许多其他选项(没有运行代码,可能是错误的,只是为了解释这个想法)。因此,任务是找到一个智能匹配模式,该模式匹配速度快。太棒了thx。我不明白为什么正则表达式在每个应答中都不同。使用正则表达式,您必须指定一个合适的模式,根据您的字符串,您可以应用多个模式来获得所需的内容,例如,从“101abc101”中提取“abc”“,您可以通过gsub(\\d,”,…)删除所有数字,或者使用捕获组gsub((^.*)(abc)(.*$),“\\2”,…)提取字母序列,或者…许多其他选项(没有运行代码,可能是错误的,只是为了解释想法)。因此,任务是找到一种快速匹配的智能匹配模式。