是否可以通過正則運算式在特定單詞之前搜索最多 3 個單詞的數值years?在下面的示例中,我之前正在搜索一個單詞years,它可以作業,但是如果您查看它回傳的第三個元素more。在這里我需要2。模式XX or more years不固定,因此我試圖找到最多 3 個單詞的數值years
Description <- c("Candidate having bachelor degree. Minimum 5 years in R", "Excellent academic background plus 3 years of experience in Python", "Analytics Professionals having minimum of 2 or more years of experience", "Candidate possessing credit risk experience plus 2 years in Python", "Candidate possessing credit risk experience plus two or more years in Python")
[1] "Candidate having bachelor degree. Minimum 5 years in R"
[2] "Excellent academic background plus 3 years of experience in Python"
[3] "Analytics Professionals having minimum of 2 or more years of experience"
[4] "Candidate possessing credit risk experience plus 2 years in Python"
[5] "Candidate possessing credit risk experience plus two or more years in Python"
代碼
str_extract(Description, "\\w (\\ )?(?= years(\\s of)?(\\s programming|experience)?\\b)")
[1] "5" "3" "more" "2 "
uj5u.com熱心網友回復:
我們可以使用命名向量將english元素替換為數字,然后進行提取
library(stringr)
library(english)
as.numeric(str_replace(str_replace_all(Description,
setNames(as.character(1:9), as.character(english(1:9)))),
".*\\b([0-9] )\\b[^0-9] \\byears.*", "\\1"))
-輸出
[1] 5 3 2 2 2
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/450186.html
上一篇:在“for”回圈中引入兩個字串
下一篇:整齊的多元單變數回歸
