Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.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
R 搜索特定的字符串模式_R_Regex_String - Fatal编程技术网

R 搜索特定的字符串模式

R 搜索特定的字符串模式,r,regex,string,R,Regex,String,我希望能够搜索具有以下格式的特定字符串: "q4-2015" "q2-2013" "q3-2011" 从一长串文件名中选择,并将其分解为两个变量: 季度和年度 例如,一长串的名字可以包括: "aaaaa-ttttt-eeee-q4-2015-file" "aaaaaa-fffff-3333-q2-2012-file" 代码应该循环遍历文件名,然后抛出特定的变量,在第一种情况下 year = 2015, quarter = q4 在第二种情况下: year = 2012, quarter =

我希望能够搜索具有以下格式的特定字符串:

"q4-2015"
"q2-2013"
"q3-2011"
从一长串文件名中选择,并将其分解为两个变量: 季度和年度

例如,一长串的名字可以包括:

"aaaaa-ttttt-eeee-q4-2015-file"
"aaaaaa-fffff-3333-q2-2012-file"
代码应该循环遍历文件名,然后抛出特定的变量,在第一种情况下

year = 2015, quarter = q4
在第二种情况下:

year = 2012, quarter = q2

etc

我们可以在这里尝试使用
sub

quarters <- sapply(input, function(x) {
    sub(".*\\b(q\\d+)-\\d{4}\\b.*", "\\1", x)
})

years <- sapply(input, function(x) {
    sub(".*\\bq\\d+-(\\d{4})\\b.*", "\\1", x)
})

df <- data.frame(quarters, years)
df

        quarters years
q4-2015       q4  2015
q2-2013       q2  2013
q3-2011       q3  2011

quarters我们可以在这里尝试使用
sub

quarters <- sapply(input, function(x) {
    sub(".*\\b(q\\d+)-\\d{4}\\b.*", "\\1", x)
})

years <- sapply(input, function(x) {
    sub(".*\\bq\\d+-(\\d{4})\\b.*", "\\1", x)
})

df <- data.frame(quarters, years)
df

        quarters years
q4-2015       q4  2015
q2-2013       q2  2013
q3-2011       q3  2011

quarters我们可以尝试这种模式

captured_words <- sub(".*\\b(q\\d)-(\\d+)\\b.*", "\\1-\\2", x)
captured_words
#[1] "q4-2015" "q2-2012"
数据

x <- c("aaaaa-ttttt-eeee-q4-2015-file","aaaaaa-fffff-3333-q2-2012-file")

x我们可以尝试这种模式

captured_words <- sub(".*\\b(q\\d)-(\\d+)\\b.*", "\\1-\\2", x)
captured_words
#[1] "q4-2015" "q2-2012"
数据

x <- c("aaaaa-ttttt-eeee-q4-2015-file","aaaaaa-fffff-3333-q2-2012-file")
x1)strcapture使用末尾注释中重复显示的测试输入,可以从基本R调用
strcapture

pat <- "(q\\d)-(\\d{4})"
strcapture(pat, x, list(quarter = "", year = 0))
另一种方法可能是使用数字四分之一列。在这种情况下,我们将使用
pat1)strcapture,使用末尾注释中重复显示的测试输入,可以从基R调用
strcapture

pat <- "(q\\d)-(\\d{4})"
strcapture(pat, x, list(quarter = "", year = 0))

另一种方法可能是使用数字四分之一列。在这种情况下,我们将使用
pat,那么您尝试了什么?你在哪里被卡住了?你试过什么?你在哪里被卡住了?回答很好+1,但你可能希望在图案的两边都添加边界。@TimBiegeleisen谢谢。然而,我不清楚界限。它做什么/阻止什么?您当前的模式将匹配,例如
xxxx-aq4-1994blah-yyyy
。但是,你不会希望这种情况发生的。边界阻止了这种情况。@user7729135在这种情况下,请删除答案中的“-”,它应该会起作用。但要确保模式始终一致。如果某些值为
f4q07“
,而另一些值为
f4q-07”
,它将不起作用。对于
sub(.*\\b(q\\d)(\\d+\\b.*”,“\\1-\\2”,x)
,您会得到什么输出?其中,
x
是您的一长串姓名。回答正确+1,但您可能希望在图案的两侧添加边界。@TimBiegeleisen谢谢。然而,我不清楚界限。它做什么/阻止什么?您当前的模式将匹配,例如
xxxx-aq4-1994blah-yyyy
。但是,你不会希望这种情况发生的。边界阻止了这种情况。@user7729135在这种情况下,请删除答案中的“-”,它应该会起作用。但要确保模式始终一致。如果某些值为
f4q07“
,而另一些值为
f4q-07”
,它将不起作用。对于
sub(.*\\b(q\\d)(\\d+\\b.*”,“\\1-\\2”,x)
,您会得到什么输出?其中
x
是您的一长串姓名。
library(gsubfn)
library(zoo)

ym  <- do.call("c", 
  strapply(x, pat, q + y ~ as.yearqtr(paste(y, q, sep = "-"))))

ym
## [1] "2015 Q4" "2012 Q2"

data.frame(quarter = paste0("q", cycle(ym), year = as.integer(ym),
  stringsAsFactors = FALSE)
##   quarter year
## 1      q4 2015
## 2      q2 2012
# test input
x <- c("aaaaa-ttttt-eeee-q4-2015-file",
  "aaaaaa-fffff-3333-q2-2012-file")