我有以下例子
x <- "carr proc proc_ proca select procb() procth;"
pattern <- "proc"
預期的結果是
"proc" "proca" "procb" "procth"
可以是串列或向量。
我用stringr::str_extract_all嘗試了幾個正則運算式,但無法得到我想要的所有單詞。
uj5u.com熱心網友回復:
利用
pattern <- "\\bproc[[:alnum:]]*\\b"
請參閱正則運算式證明。
解釋
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
proc 'proc'
--------------------------------------------------------------------------------
[[:alnum:]]* any character of: letters and digits (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
uj5u.com熱心網友回復:
那這個呢?
> unique(agrep(pattern, unlist(strsplit(x, "[^[:alpha:]] ")), value = TRUE))
[1] "proc" "proca" "procb" "procth"
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/390423.html
下一篇:在R中旋轉更寬的一行
