对于R,假设我具有以下字符向量:
input_vector <- c("123abc", "456efg", "hij789", "lmn000")
我想知道是否有一个现有的函数,比如myfunc()
,可以输出我的向量后面的正则表达式模式.换句话说:
myfunc(input_vector)
> [1] "[:digit:]{3}[:alpha:]{3}" "[:digit:]{3}[:alpha:]{3}" "[:alpha:]{3}[:digit:]{3}"
[4] "[:alpha:]{3}[:digit:]{3}"
[EDIT]:在@Allan Cameron的第一个响应之后,我补充说,我的最终目标是检测在任意长度的字符向量中找到的所有可能的[:digit:]-[:alpha:]
个模式,我知道它的元素理论上只嵌入[:digit:]
或[:alpha:]
个字符(这是一个简化,但这个问题的解决方案在很大程度上已经足够了).请说:
input_vector2 <- 1000L
input_vector2 <- c("123abc", "456efg", ..., "zz6ab8") # this line is illustrative, just imagine a 1000L character vector
如果可能的话,我真正想要的是一个函数,它返回input_vector2
的元素级[:Digit:]-[:Alpha:] struct :
myfunc(input_vector2)
> [1] "[:digit:]{3}[:alpha:]{3}" # (or any way to tell there is three consecutive [:digit:] then three consecutive [:alpha:])
[2] "[:digit:]{3}[:alpha:]{3}"
...
[1000] "[:alpha:]{2}[:digit:][:alpha:]{2}[:digit:]"
Thanks in all cases!
Daniel
我试图搜索现有的堆栈溢出相关主题,但找不到解决方案,也找不到解决方案的提示.
我想不出任何我知道的R函数/技巧可以得到上面提到的期望输出.