对于 R,假设我有以下字符向量:
input_vector <- c("123abc", "456efg", "hij789", "lmn000")
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一个现有的函数,例如myfunc(),可以输出我的向量后面的正则表达式模式。换句话说:
myfunc(input_vector)
> [1] "[:digit:]{3}[:alpha:]{3}" "[:digit:]{3}[:alpha:]{3}" "[:alpha:]{3}[:digit:]{3}"
[4] "[:alpha:]{3}[:digit:]{3}"
Run Code Online (Sandbox Code Playgroud)
[编辑]:在@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
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我真正想要的是一个返回以下元素的 [:digit:]-[:alpha:] 结构的函数input_vector2:
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:]"
Run Code Online (Sandbox Code Playgroud)
在所有情况下都表示感谢!
丹尼尔
我尝试搜索现有的堆栈溢出相关主题,但找不到解决方案,也找不到解决方案的提示。 …